This commit is contained in:
han0
2024-05-29 10:21:31 +08:00
commit 54ac29d27b
119 changed files with 6817 additions and 0 deletions

116
web/worker/__init__.py Normal file
View File

@@ -0,0 +1,116 @@
import gevent
from gevent import monkey
import threading
import time
from config import DEBUG
MODE_GEVENT = 'gevent'
MODE_THREADING = 'threading'
ASYNC_MODE = MODE_THREADING if DEBUG else MODE_GEVENT
class GeventWorker:
def __init__(self):
self.init()
def monkey_patch(self):
monkey.patch_all()
def spawn(self, *args, **kwargs):
return gevent.spawn(*args, **kwargs)
def patch_greenlet(self, f):
def inner(*args, **kwargs):
return self.spawn(f, *args, **kwargs)
return inner
def joinall(self, threads):
gevent.joinall(threads)
def is_alive(self, thread):
alive = False
if thread:
alive = not thread.ready()
return alive
def kill(self, thread):
if thread:
return thread.kill()
return None
def sleep(self, seconds=0):
gevent.sleep(seconds)
def init(self):
self.monkey_patch()
def lock(self):
return gevent.lock.RLock()
class ThreadingWorker:
@classmethod
def spawn(cls, *args, **kwargs):
thread = threading.Thread(target=args[0], args=args[1:], kwargs=kwargs)
thread.start()
return thread
@classmethod
def patch_greenlet(cls, f):
def inner(*args, **kwargs):
return cls.spawn(f, *args, **kwargs)
return inner
@classmethod
def joinall(cls, threads):
for thread in threads:
thread.join()
@classmethod
def is_alive(cls, thread):
alive = False
if thread:
alive = thread.is_alive()
return alive
@classmethod
def sleep(cls, seconds=0):
time.sleep(seconds)
@classmethod
def init(cls):
pass
@classmethod
def kill(cls, thread):
if thread:
return thread.join(0)
return None
@classmethod
def lock(cls):
return threading.RLock()
if ASYNC_MODE == MODE_GEVENT:
patch = GeventWorker()
from gevent.queue import Queue, Empty
else:
patch = ThreadingWorker()
from six.moves.queue import Queue, Empty
spawn = patch.spawn
patch_greenlet = patch.patch_greenlet
joinall = patch.joinall
is_alive = patch.is_alive
sleep = patch.sleep
init = patch.init
kill = patch.kill
lock = patch.lock