This commit is contained in:
Ra 2021-09-27 08:26:05 -07:00 committed by GitHub
parent d913f561bc
commit 3af9d78008
1 changed files with 48 additions and 14 deletions

View File

@ -1,20 +1,54 @@
from queue import Queue
from threading import Thread
def threaded_task(fn):
queue = Queue()
def wrapper(queue_size):
queue.max_size = queue_size
def wrapped(*args_, **kwargs_):
queue.put((args_, kwargs_))
return wrapped
fn.queue = Queue()
def worker():
while True:
args, kwargs = queue.get()
fn(*args, **kwargs)
queue.task_done()
self, num = fn.queue.get()
fn(self, num)
fn.queue.task_done()
Thread(target=worker)
if not getattr(fn, "thread", None):
fn.thread = Thread(target=worker, daemon=True).start()
return wrapper
def wrapper(*args, **kwargs):
print(args, kwargs)
if args:
self, num = args
fn.queue.put((self, num))
if maxsize := kwargs.get("maxsize", None):
fn.queue.maxsize = maxsize
return
wrapper.queue = fn.queue
wrapper.fn = fn
return wrapper
class Job:
"""Thread executing tasks from a given tasks queue"""
@threaded_task
def print_number(self, number):
print(number)
if __name__ == "__main__":
import inspect
numbers = [i for i in range(1000)]
Job.print_number(maxsize=100)
jobs = []
for num in numbers:
jobs.append((new_job := Job()))
new_job.print_number(num)
Job.print_number.queue.join()