简体   繁体   English

python多处理池阻塞主线程

[英]python multiprocessing pool blocking main thread

I have the following snippet which attempts to split processing across multiple sub-processes.我有以下代码段,它尝试跨多个子流程拆分处理。

def search(self):
    print("Checking queue for jobs to process")
    if self._job_queue.has_jobs_to_process():

        print("Queue threshold met, processing jobs.")
        job_sub_lists = partition_jobs(self._job_queue.get_jobs_to_process(), self._process_pool_size)
        populated_sub_lists =  [sub_list for sub_list in job_sub_lists if len(sub_list) > 0]
        self._process_pool.map(process, populated_sub_lists)
        print("Job processing pool mapped")

The search function is being called by the main process in a while loop and if the queue reaches a threshold count, the processing pool is mapped to the process function with the jobs sourced from the queue.主进程在 while 循环中调用搜索函数,如果队列达到阈值计数,则处理池将映射到进程函数,其中包含来自队列的作业。 My question is, does the python multiprocessing pool block the main process during execution or does it immediately continue execution?我的问题是,python 多处理池是在执行期间阻塞主进程还是立即继续执行? I don't want to encounter the scenario where "has_jobs_to_process()" evaluates to true and during the processing of the jobs, it evaluates to true for another set of jobs and "self._process_pool.map(process, populated_sub_lists)" is called again as I do not know the consequences of calling map again while processes are running.我不想遇到“has_jobs_to_process()”评估为真的情况,在处理作业的过程中,它为另一组作业评估为真,并调用“self._process_pool.map(process,populated_sub_lists)”再次因为我不知道在进程运行时再次调用 map 的后果。

multiprocessing.Pool.map blocks the calling thread (not necessarily the MainThread!), not the whole process. multiprocessing.Pool.map阻塞调用线程(不一定是 MainThread!),而不是整个进程。 Other threads of the parent process will not be blocked.父进程的其他线程不会被阻塞。 You could call pool.map from multiple threads in the parent process without breaking things (doesn't make much sense, though).您可以从父进程中的多个线程调用pool.map而不会破坏事物(虽然没有多大意义)。 That's because Pool uses thread-safe queue.Queue internally for it's _taskqueue .那是因为Pool内部使用线程安全queue.Queue作为_taskqueue

从 multiprocessing 文档中, multiprocessing.map将在执行期间阻塞主进程,直到结果准备好,而multiprocessing.map_async不会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM