Multiprocessing Advice

后端 未结 2 1304
眼角桃花
眼角桃花 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:22

    To ping multiple ip addresses concurrently is easy using multiprocessing:

    #!/usr/bin/env python
    from multiprocessing.pool import ThreadPool # use threads
    from subprocess import check_output
    
    def ping(ip, timeout=10):
        cmd = "ping -c4 -n -w {timeout} {ip}".format(**vars())
        try:
            result = check_output(cmd.split())
        except Exception as e:
            return ip, None, str(e)
        else:
            return ip, result, None
    
    pool = ThreadPool(100) # no more than 100 pings at any single time
    for ip, result, error in pool.imap_unordered(ping, ip_list):
        if error is None: # no error
           print(ip) # print ips that have returned 4 packets in timeout seconds
    

    Note: I've used ThreadPool here as a convient way to limit number of concurrent pings. If you want to do all pings at once then you don't need neither threading nor multiprocessing modules because each ping is already in its own process. See Multiple ping script in Python.

提交回复
热议问题