mirror of
https://github.com/simon987/hexlib.git
synced 2025-04-20 02:06:42 +00:00
concurrency
This commit is contained in:
parent
7633c64057
commit
3db293c867
47
hexlib/concurrency.py
Normal file
47
hexlib/concurrency.py
Normal 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()
|
2
setup.py
2
setup.py
@ -2,7 +2,7 @@ from setuptools import setup
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="hexlib",
|
name="hexlib",
|
||||||
version="1.9",
|
version="1.10",
|
||||||
description="Misc utility methods",
|
description="Misc utility methods",
|
||||||
author="simon987",
|
author="simon987",
|
||||||
author_email="me@simon987.net",
|
author_email="me@simon987.net",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user