Changeset 1025

Show
Ignore:
Timestamp:
01/22/12 08:59:56 (4 months ago)
Author:
hodgestar
Message:

Add synclock implementation.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • hodgestar/PythonCode/Twistify/twistify.py

    r1024 r1025  
    55from functools import partial 
    66 
    7 from twisted.internet.defer import inlineCallbacks 
     7from twisted.internet.defer import inlineCallbacks, DeferredLock 
    88from twisted.internet.threads import deferToThread 
    99from twisted.internet import reactor 
     
    2323    :param whitelist: 
    2424        Names of attributes that don't need to be wrapped. 
     25        Default: [] 
    2526    :type blacklist: list 
    2627    :param blacklist: 
    2728        Names of attributes that should themselves be 
    2829        wrapped by a Twistify object. 
     30        Default: [] 
     31    :type synclock: bool 
     32    :param synclock: 
     33        If True, only allow one method to execute in a thread 
     34        at a time (later calls only begin one earlier calls are done). 
     35        Default: False 
    2936    """ 
    3037 
    31     def __init__(self, wrapped, whitelist=(), blacklist=()): 
     38    def __init__(self, wrapped, whitelist=(), blacklist=(), synclock=False): 
    3239        self.__wrapped = wrapped 
    3340        self.__whitelist = set(whitelist) 
    3441        self.__blacklist = set(blacklist) 
     42        self.__synclock = None if not synclock else DeferredLock() 
    3543 
    3644    def __call_in_thread(self, f, *args, **kwargs): 
    37         return deferToThread(f, *args, **kwargs) 
     45        if self.__synclock is None: 
     46            return deferToThread(f, *args, **kwargs) 
     47        else: 
     48 
     49            def call_f(lock): 
     50                return deferToThread(f, *args, **kwargs) 
     51 
     52            def release_lock(result): 
     53                self.__synclock.release() 
     54                return result 
     55 
     56            locked = self.__synclock.acquire() 
     57            locked.addCallback(call_f) 
     58            locked.addBoth(release_lock) 
     59            return locked 
    3860 
    3961    def __getattr__(self, name): 
     
    7698    print "Test object awake." 
    7799 
     100    print "---" 
     101 
    78102    print "Testing return of a value ..." 
    79103    d = proxy.echo("Echo echo echo.") 
     
    81105    print "Got back: %r." % (v,) 
    82106 
     107    print "---" 
     108 
     109    SyncTestObject = twistify_type(TestObject, synclock=True) 
     110    synced_proxy = SyncTestObject() 
     111 
     112    print "Testing synclock ..." 
     113    d1 = synced_proxy.sleep(2) 
     114    d2 = synced_proxy.echo("Echo ...") 
     115    v = yield d2 
     116    print "Got back: %r." % (v,) 
     117    print "Sleep done:", d1.called 
     118 
     119    print "---" 
     120 
     121 
     122def stop_reactor(result): 
     123    reactor.stop() 
     124    return result 
    83125 
    84126if __name__ == "__main__": 
    85     main().addBoth(lambda result: reactor.stop()) 
     127    main().addBoth(stop_reactor) 
    86128    reactor.run()