<s> from __future__ import with_statement <EOL> import time , select , errno <EOL> from gearman . compat import * <EOL> from gearman . connection import GearmanConnection <EOL> from gearman . task import Task , Taskset <EOL> from gearman . client import GearmanClient <EOL> from twisted . internet import abstract , defer , reactor <EOL> class _ConnectionWrapper ( abstract . FileDescriptor ) : <EOL> def __init__ ( self , conn , onRead , * args , ** kwargs ) : <EOL> abstract . FileDescriptor . __init__ ( self , * args , ** kwargs ) <EOL> self . conn = conn <EOL> self . onRead = onRead <EOL> self . reading = False <EOL> self . writing = False <EOL> def startReading ( self ) : <EOL> if not self . reading : <EOL> reactor . addReader ( self ) <EOL> self . reading = True <EOL> def stopReading ( self ) : <EOL> if self . reading : <EOL> reactor . removeReader ( self ) <EOL> self . reading = False <EOL> def startWriting ( self ) : <EOL> if not self . writing : <EOL> reactor . addWriter ( self ) <EOL> self . writing = True <EOL> def stopWriting ( self ) : <EOL> if self . writing : <EOL> reactor . removeWriter ( self ) <EOL> self . writing = False <EOL> def setState ( self ) : <EOL> if self . conn . readable ( ) : <EOL> self . startReading ( ) <EOL> else : <EOL> self . stopReading ( ) <EOL> if self . conn . writable ( ) : <EOL> self . startWriting ( ) <EOL> else : <EOL> self . stopWriting ( ) <EOL> def __enter__ ( self ) : <EOL> self . setState ( ) <EOL> def __exit__ ( self , type , value , traceback ) : <EOL> self . stopReading ( ) <EOL> self . stopWriting ( ) <EOL> def doWrite ( self ) : <EOL> self . conn . send ( ) <EOL> self . setState ( ) <EOL> def doRead ( self ) : <EOL> for cmd in self . conn . recv ( ) : <EOL> self . onRead ( * cmd ) <EOL> self . setState ( ) <EOL> def fileno ( self ) : <EOL> return self . conn . fileno ( ) <EOL> class Client ( GearmanClient ) : <EOL> @ defer . inlineCallbacks <EOL> def do_task ( self , task ) : <EOL> """<STR_LIT>""" <EOL> rtn = defer . Deferred ( ) <EOL> def _on_fail ( ) : <EOL> rtn . errback ( self . TaskFailed ( "<STR_LIT>" ) ) <EOL> task . on_fail . append ( _on_fail ) <EOL> task . on_complete . append ( rtn . callback ) <EOL> taskset = Taskset ( [ task ] ) <EOL> taskset . handles = { } <EOL> c = self . _submit_task ( task ) <EOL> taskset . connections = set ( [ c ] ) <EOL> def _do_cmd ( * args ) : <EOL> return self . _command_handler ( taskset , c , * args ) <EOL> with _ConnectionWrapper ( c , _do_cmd ) : <EOL> rtn = yield rtn <EOL> defer . returnValue ( rtn ) <EOL> def dispatch_background_task ( self , func , arg , uniq = None , high_priority = False ) : <EOL> assert False , '<STR_LIT>' <EOL> def do_taskset ( self , taskset , timeout = None ) : <EOL> assert False , '<STR_LIT>' <EOL> def get_status ( self , handle ) : <EOL> assert False , '<STR_LIT>' </s>
