How to use SyncManager.Lock or Event correctly?

后端 未结 1 731
执念已碎
执念已碎 2021-01-07 02:21

I\'m having trouble using SyncManager.Lock correctly. I read the official doc, but it offers no working example. I also have no idea how to use SyncManage

相关标签:
1条回答
  • 2021-01-07 02:31

    I figured out a workaround. Don't use the builtin SyncManager.Lock() for the following reasons:

    1. It's creating a new Lock object every time instead of sharing.
    2. It wraps around threading.Lock(), NOT multiprocess.Lock(). Looks like it doesn't work with multiprocessing!

    Solution is to register your own lock manager:

    from multiprocessing.managers import BaseManager, AcquirerProxy
    global_lock = mp.Lock()
    
    def get_lock():
        print('getting global_lock')
        return global_lock
    
    Server.register('Lock', get_lock, AcquirerProxy)
    
    0 讨论(0)
提交回复
热议问题