简体   繁体   English

如何在 Python 的循环内将一个进程的执行与另一个进程联系起来

[英]How to to tie the execution of one process to another inside a loop in Python

How can I keep a loop going, while having some processes that are waiting for others inside the loop?如何保持循环继续进行,同时让一些进程在循环中等待其他进程? (see the code below for clarification, it makes more sense to explain in code) (请参阅下面的代码进行澄清,在代码中解释更有意义)

for i in range(0, len(my_list), batch_size):
    current_batch = my_list[i:i+batch_size]
    download_processes = [Popen('do stuff')] # NOT ACTUAL CODE. This downloads files.
    _ = [p.wait() for p in download_processes] # wait until all files above download before executing the code below 

    more_processes = [Popen('do stuff')] # NOT ACTUAL CODE. This zips the downloaded files
    # execute yet more processes (that process the zips files) when "more_processes" end, but keep the loop going to start downloading more files
  1. Create a pool创建一个池

  2. Use loop inside pool for the batch在池中使用循环进行批处理

  3. Use result = pool.map_async() with your target method将 result = pool.map_async() 与您的目标方法一起使用

4.Do result.get(timeout) or result.wait() 4. 执行 result.get(timeout) 或 result.wait()

  1. If you get timeout or after your condition is met, return and break out of the while loop and pool.close, terminate and then join.如果超时或满足条件后,返回并跳出 while 循环和 pool.close,终止然后加入。

def process_url(url):
  # call url and process data
  
  pass

def pool_handler():
    with Pool() as p:
     for i in range(0, len(my_list), batch_size):
      current_batch_urls = my_list[i:i+batch_size]
    
      # this will create processes and process url
      r = p.map_async(process_url, current_batch_urls)
      r. wait()#wait for each batch

   #outside loop 
    p.close()
    p.join()#wait until all processes are done

if __name__ == '__main__':
    pool_handler()


You can use multiprocessing module to achieve this您可以使用multiprocessing模块来实现这一点

from multiprocessing import Pool
import time, requests

urls = ["file_url1","file_url2","file_url3"]


def download_file(url):
    return requests.get(url).content.strip()

def process_url(url):
    file_content = download_file(url)
    # Process File content 

def pool_handler():
    p = Pool(2)
    p.map(process_url, urls)

if __name__ == '__main__':
    pool_handler()

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

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