Multi-process and gevent loop friendly lock

This commit is contained in:
shortcutme 2019-11-30 02:07:30 +01:00
parent b7c6b84826
commit 66a1c4d242
No known key found for this signature in database
GPG key ID: 5B63BAE6CB9613AE
2 changed files with 67 additions and 1 deletions

View file

@ -1,4 +1,6 @@
import gevent.threadpool
import gevent._threading
import threading
class ThreadPool:
@ -17,6 +19,24 @@ class ThreadPool:
return func
def wrapper(*args, **kwargs):
return self.pool.apply(func, args, kwargs)
res = self.pool.apply(func, args, kwargs)
return res
return wrapper
main_thread_id = threading.current_thread().ident
lock_pool = gevent.threadpool.ThreadPool(10)
class Lock:
def __init__(self):
self.lock = gevent._threading.Lock()
self.locked = self.lock.locked
self.release = self.lock.release
def acquire(self, *args, **kwargs):
if self.locked() and threading.current_thread().ident == main_thread_id:
return lock_pool.apply(self.lock.acquire, args, kwargs)
else:
return self.lock.acquire(*args, **kwargs)