Multiprocessing Advice

后端 未结 2 1312
眼角桃花
眼角桃花 2021-01-28 08:03

I\'ve been trying to get my head around multiprocessing. The problem is all the examples I\'ve come across don\'t seem to fit my scenario. I\'d like to multiprocess or thread wo

2条回答
  •  臣服心动
    2021-01-28 08:31

    What you are currently doing should pose no problem, but if you want to manually create the processes and then join them later on:

    import subprocess
    import multiprocessing as mp
    
    
    # Creating our target function here
    def do_work(args):
        # dummy function
        p = subprocess.check_output(["ping", "-c", "4", ip])
        print(p)
    
    # Your ip list
    ip_list = ['8.8.8.8', '8.8.4.4']
    
    procs = []  # Will contain references to our processes
    for ip in ip_list:
        # Creating a new process
        p = mp.Process(target=do_work, args=(ip,))
    
        # Appending to procs
        procs.append(p)
    
        # starting process
        p.start()
    
    # Waiting for other processes to join
    for p in procs:
        p.join()
    

提交回复
热议问题