<s> import json <EOL> import threading <EOL> import traceback <EOL> import zmq <EOL> import mayaserver <EOL> from maya . utils import executeInMainThreadWithResult <EOL> def runserver ( handshake_port ) : <EOL> threading . Thread ( <EOL> target = _runserver , args = [ handshake_port ] ) . start ( ) <EOL> def _eval ( s ) : <EOL> return eval ( s , globals ( ) , globals ( ) ) <EOL> def _exec ( s ) : <EOL> exec s in globals ( ) , globals ( ) <EOL> def _runserver ( handshake_port ) : <EOL> sock = zmq . Context ( ) . socket ( zmq . REP ) <EOL> appport = sock . bind_to_random_port ( '<STR_LIT>' ) <EOL> handshakesock = zmq . Context ( ) . socket ( zmq . REQ ) <EOL> handshakesock . connect ( '<STR_LIT>' % handshake_port ) <EOL> handshakesock . send ( str ( appport ) ) <EOL> handshakesock . recv ( ) <EOL> handshakesock . close ( ) <EOL> while True : <EOL> recved = sock . recv ( ) <EOL> func , arg = json . loads ( recved ) <EOL> try : <EOL> code = mayaserver . SUCCESS <EOL> response = None <EOL> if func == '<STR_LIT>' : <EOL> executeInMainThreadWithResult ( _exec , arg ) <EOL> elif func == '<STR_LIT>' : <EOL> response = executeInMainThreadWithResult ( <EOL> _eval , arg ) <EOL> else : <EOL> code = mayaserver . INVALID_METHOD <EOL> response = func <EOL> pickled = json . dumps ( [ code , response ] ) <EOL> except Exception : <EOL> pickled = json . dumps ( [ <EOL> mayaserver . UNHANDLED_ERROR , <EOL> '<STR_LIT>' . join ( traceback . format_exc ( ) ) ] ) <EOL> sock . send ( pickled ) </s>
