本文整理汇总了Python中multiprocessing.JoinableQueue方法的典型用法代码示例。如果您正苦于以下问题:Python multiprocessing.JoinableQueue方法的具体用法?Python multiprocessing.JoinableQueue怎么用?Python multiprocessing.JoinableQueue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multiprocessing
的用法示例。
在下文中一共展示了multiprocessing.JoinableQueue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _producer_multi_threads
# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import JoinableQueue [as 别名]
def _producer_multi_threads(queue_task, queue_product, worker_function):
"""
负责在本进程内分发多线程任务
:type queue_task: multiprocessing.JoinableQueue
:type queue_product: multiprocessing.JoinableQueue
:type worker_function: Callable[[Any], Any]
"""
while True:
try:
task = queue_task.get()
if isinstance(task, _QueueEndSignal): # 结束信号
# finally 里的 task_done() 在break的情况下仍然会被执行
break
if isinstance(task, dict):
result = worker_function(**task)
elif isinstance(task, (tuple, list)):
result = worker_function(*task)
else:
result = worker_function(task)
queue_product.put((task, result))
except:
traceback.print_exc()
finally:
queue_task.task_done()
示例2: scale
# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import JoinableQueue [as 别名]
def scale(size, smooth, source, target, concurrency):
canceled = False
jobs = multiprocessing.JoinableQueue()
results = multiprocessing.Queue()
create_processes(size, smooth, jobs, results, concurrency)
todo = add_jobs(source, target, jobs)
try:
jobs.join()
except KeyboardInterrupt: # May not work on Windows
Qtrac.report("canceling...")
canceled = True
copied = scaled = 0
while not results.empty(): # Safe because all jobs have finished
result = results.get_nowait()
copied += result.copied
scaled += result.scaled
return Summary(todo, copied, scaled, canceled)
示例3: __init__
# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import JoinableQueue [as 别名]
def __init__(self,
tasks_queue_in: multiprocessing.JoinableQueue,
num_proc: int,
pca_numbers: Union[np.ndarray, tuple]) -> None:
"""
Parameters
----------
tasks_queue_in : multiprocessing.queues.JoinableQueue
Input task queue.
num_proc : int
Number of processors.
pca_numbers : np.ndarray, tuple
Principal components for which the residuals are computed.
Returns
-------
NoneType
None
"""
super(PcaTaskCreator, self).__init__(None, tasks_queue_in, None, num_proc)
self.m_pca_numbers = pca_numbers
示例4: __init__
# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import JoinableQueue [as 别名]
def __init__(self,
tasks_queue_in: multiprocessing.JoinableQueue,
result_queue_in: multiprocessing.JoinableQueue) -> None:
"""
Parameters
----------
tasks_queue_in : multiprocessing.queues.JoinableQueue
The input task queue with instances of :class:`~pynpoint.util.multiproc.TaskInput`.
result_queue_in : multiprocessing.queues.JoinableQueue
The result task queue with instances of :class:`~pynpoint.util.multiproc.TaskResult`.
Returns
-------
NoneType
None
"""
multiprocessing.Process.__init__(self)
self.m_task_queue = tasks_queue_in
self.m_result_queue = result_queue_in
示例5: get_multi_q
# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import JoinableQueue [as 别名]
def get_multi_q(self, sentinel='STOP'):
'''
This helps indexq operate in multiprocessing environment without each process having to have it's own IndexQ. It also is a handy way to deal with thread / process safety.
This method will create and return a JoinableQueue object. Additionally, it will kick off a back end process that will monitor the queue, de-queue items and add them to this indexq.
The returned JoinableQueue object can be safely passed to multiple worker processes to populate it with data.
To indicate that you are done writing the data to the queue, pass in the sentinel value ('STOP' by default).
Make sure you call join_indexer() after you are done to close out the queue and join the worker.
'''
self.in_q = JoinableQueue()
self.indexer_process = Process(target=self._indexer_process, args=(self.in_q, sentinel))
self.indexer_process.daemon = False
self.indexer_process.start()
return self.in_q
示例6: __init__
# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import JoinableQueue [as 别名]
def __init__(
self,
queue: JoinableQueue,
arguments: str,
ports: str,
sudo: bool,
hosts_quantity: int,
results_pool: dict,
):
Process.__init__(self)
self.queue = queue
self.arguments = arguments
self.ports = ports
self.sudo = sudo
self.quantity = hosts_quantity
self.results_pool = results_pool
示例7: test_task_done
# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import JoinableQueue [as 别名]
def test_task_done(self):
queue = self.JoinableQueue()
workers = [self.Process(target=self._test_task_done, args=(queue,))
for i in range(4)]
for p in workers:
p.daemon = True
p.start()
for i in range(10):
queue.put(i)
queue.join()
for p in workers:
queue.put(None)
for p in workers:
p.join()
示例8: use_multiprocessing_with_queue2
# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import JoinableQueue [as 别名]
def use_multiprocessing_with_queue2():
queue = multiprocessing.JoinableQueue()
num_consumers = multiprocessing.cpu_count() * 2
results_queue = multiprocessing.Queue()
for article in Article.objects.all()[5:8]:
queue.put(article)
for _ in range(num_consumers):
p = multiprocessing.Process(target=save_article_result_with_queue2,
args=(queue, results_queue))
p.start()
queue.join()
results = []
while 1:
try:
updated_article = results_queue.get(timeout=1)
except Empty:
break
results.append(updated_article)
print len(results)
示例9: use_multiprocessing_with_queue2
# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import JoinableQueue [as 别名]
def use_multiprocessing_with_queue2():
queue = multiprocessing.JoinableQueue()
num_consumers = multiprocessing.cpu_count() * 2
results_queue = multiprocessing.Queue()
for article in Article.objects.all():
queue.put(article)
for _ in range(num_consumers):
p = multiprocessing.Process(target=save_article_result_with_queue2,
args=(queue, results_queue))
p.start()
queue.join()
results = []
while 1:
try:
updated_article = results_queue.get(timeout=1)
except Empty:
break
results.append(updated_article)
print len(results)
示例10: __init__
# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import JoinableQueue [as 别名]
def __init__(self, inFile, outFile, processcount=None):
"""
Initiate controller procedure
:param inFile: the file containing the URLs
:param outFile: the output file, "result.txt" by default
"""
try:
self.urllist = deduplicate(FileReader(inFile).read()).result
self.workerCount = int(processcount) if processcount else multiprocessing.cpu_count() * 2
self.taskQ = multiprocessing.JoinableQueue()
self.resultQ = multiprocessing.Queue()
self.workers = []
self.outfile = outFile
self.start()
logging.info("[+] All work done, saving file")
except KeyboardInterrupt:
pass
finally:
self.cleanup()
示例11: _ensure_async
# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import JoinableQueue [as 别名]
def _ensure_async(self):
"""
Ensure that the asynchronous execution infrastructure is up
and the worker process is running
"""
if self.queue:
return
self.queue = multiprocessing.JoinableQueue(
maxsize=self.worker_processes_count)
self.result_queue = multiprocessing.Queue()
self.errors_queue = multiprocessing.Queue()
self.done_queue = multiprocessing.Queue()
for process_number in range(self.worker_processes_count):
process = multiprocessing.Process(
target=self.worker_process_main,
args=(process_number,))
process.start()
self.worker_processes.append(process)
示例12: add_evaluator_workers
# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import JoinableQueue [as 别名]
def add_evaluator_workers(self):
"""Add evaluator workers
Evaluator workers receive all graph updates, hence are updated.
Each evaluator worker holds an enabled scenario-evaluator and process
every change.
Each worker's scenario-evaluator runs different template scenarios.
Interface to these workers is:
submit_graph_update(..)
submit_start_evaluations(..)
submit_evaluators_reload_templates(..)
"""
if self._evaluator_queues:
raise VitrageError('add_evaluator_workers called more than once')
workers = CONF.evaluator.workers
queues = [multiprocessing.JoinableQueue() for i in range(workers)]
self.add(EvaluatorWorker,
args=(queues, workers),
workers=workers)
self._evaluator_queues = queues
self._all_queues.extend(queues)
示例13: refactor
# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import JoinableQueue [as 别名]
def refactor(self, items, write=False, doctests_only=False,
num_processes=1):
if num_processes == 1:
return super(MultiprocessRefactoringTool, self).refactor(
items, write, doctests_only)
try:
import multiprocessing
except ImportError:
raise MultiprocessingUnsupported
if self.queue is not None:
raise RuntimeError("already doing multiple processes")
self.queue = multiprocessing.JoinableQueue()
self.output_lock = multiprocessing.Lock()
processes = [multiprocessing.Process(target=self._child)
for i in range(num_processes)]
try:
for p in processes:
p.start()
super(MultiprocessRefactoringTool, self).refactor(items, write,
doctests_only)
finally:
self.queue.join()
for i in range(num_processes):
self.queue.put(None)
for p in processes:
if p.is_alive():
p.join()
self.queue = None
示例14: refactor
# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import JoinableQueue [as 别名]
def refactor(self, items, write=False, doctests_only=False,
num_processes=1):
if num_processes == 1:
return super(MultiprocessRefactoringTool, self).refactor(
items, write, doctests_only)
try:
import multiprocessing
except ImportError:
raise MultiprocessingUnsupported
if self.queue is not None:
raise RuntimeError("already doing multiple processes")
self.queue = multiprocessing.JoinableQueue()
self.output_lock = multiprocessing.Lock()
processes = [multiprocessing.Process(target=self._child)
for i in xrange(num_processes)]
try:
for p in processes:
p.start()
super(MultiprocessRefactoringTool, self).refactor(items, write,
doctests_only)
finally:
self.queue.join()
for i in xrange(num_processes):
self.queue.put(None)
for p in processes:
if p.is_alive():
p.join()
self.queue = None
示例15: _producer_multi_processes
# 需要导入模块: import multiprocessing [as 别名]
# 或者: from multiprocessing import JoinableQueue [as 别名]
def _producer_multi_processes(queue_task,
queue_product,
threads_per_process,
worker_function):
"""
接收与多进程任务并分发给子线程
:type queue_task: multiprocessing.JoinableQueue
:type queue_product: multiprocessing.JoinableQueue
:type threads_per_process: int
:type worker_function: Callable[[Any], Any]
"""
_queue_task = queue.Queue(maxsize=threads_per_process)
_queue_product = queue.Queue()
pool = [threading.Thread(target=_producer_multi_threads, args=(_queue_task, _queue_product, worker_function))
for _ in range(threads_per_process)]
for t in pool:
t.daemon = True
t.start()
th = threading.Thread(target=_subprocesses_queue_transfer, args=(queue_task, _queue_task))
th.daemon = True
th.start()
th = threading.Thread(target=_subprocesses_queue_transfer, args=(_queue_product, queue_product))
th.daemon = True
th.start()
# 等待所有子线程结束
for t in pool:
t.join()
logger.debug("subthread {} of {} stopped".format(t.name, multiprocessing.current_process().name))
logger.debug("subprocess {} completed".format(multiprocessing.current_process().name))