mirror of
https://github.com/simon987/hexlib.git
synced 2025-04-10 14:06:43 +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()
|
Loading…
x
Reference in New Issue
Block a user