concurrency

This commit is contained in:
simon987 2020-06-14 10:29:12 -04:00
parent 7633c64057
commit 3db293c867
2 changed files with 48 additions and 1 deletions

47
hexlib/concurrency.py Normal file
View File

@ -0,0 +1,47 @@
from queue import Queue, Empty
from multiprocessing import Process
from threading import Thread
def queue_iter(q: Queue, **get_args):
while True:
try:
task = q.get(**get_args)
if task is None:
break
yield task
q.task_done()
except Empty:
break
except KeyboardInterrupt:
break
def queue_thread_exec(q, func, thread_count=4):
threads = []
for _ in range(thread_count):
t = Thread(target=func, args=(q,))
threads.append(t)
t.start()
q.join()
for t in threads:
t.join()
def queue_process_exec(q, func, process_count=4):
processes = []
for _ in range(process_count):
p = Process(target=func, args=(q,))
processes.append(p)
p.start()
q.join()
for p in processes:
p.join()

View File

@ -2,7 +2,7 @@ from setuptools import setup
setup(
name="hexlib",
version="1.9",
version="1.10",
description="Misc utility methods",
author="simon987",
author_email="me@simon987.net",