使用共享变量停止子进程

/*使用共享变量停止子进程*/
from multiprocessing import Process, Value from collections import namedtuple import time import sys s = time.time() def worker1(alive): while alive.value: time.sleep(0.5) print(sys._getframe().f_code.co_name) print("%s %s sec" % (sys._getframe().f_code.co_name, (time.time() - s))) def worker2(alive): while alive.value: time.sleep(1) print(sys._getframe().f_code.co_name) print("%s %s sec" % (sys._getframe().f_code.co_name, (time.time() - s))) def worker3(alive): while alive.value: time.sleep(1) print("%s %s sec" % (sys._getframe().f_code.co_name, (time.time() - s))) def process(workerlist): plist = [] alive = Value('b', False) alive.value = True for worker in workerlist: p = Process(target=worker.function, args=(alive,)) plist.append((p,worker.timeout)) p.start() print("%s %s sec" % (sys._getframe().f_code.co_name, (time.time() - s))) for p,timeout in plist: p.join(timeout) alive.value = False if __name__ == '__main__': Worker = namedtuple('Worker','function timeout') workerlist = [Worker(worker1, 2), Worker(worker2, 2), Worker(worker3, 3),] s = time.time() process(workerlist) print("end %s sec" % (time.time() - s))
from multiprocessing import Process, Value, Manager
from collections import namedtuple
import time
import sys
class T():

    def worker1(self, alive, s, return_dict):
      while alive.value:
        time.sleep(0.5)
        print(sys._getframe().f_code.co_name)
        func = sys._getframe().f_code.co_name
        print("%s %s sec" % (func, (time.time() - s)))
        if (time.time() - s)>2:
            return_dict[func] = '1'
            return return_dict

    def worker2(self, alive, s, return_dict):
      while alive.value:
        time.sleep(1)
        print(sys._getframe().f_code.co_name)
        func = sys._getframe().f_code.co_name
        print("%s %s sec" % (func, (time.time() - s)))
        if (time.time() - s)>2:
            return_dict[func] = '2'
            return return_dict

    def worker3(self, alive, s, return_dict):
      while alive.value:
        time.sleep(1)
        func = sys._getframe().f_code.co_name
        print("%s %s sec" % (func, (time.time() - s)))
        if (time.time() - s)>5:
            return_dict[func] = '3'
            return return_dict

    def process(self):
        manager = Manager()
        return_dict = manager.dict()
        s = time.time()
        Worker = namedtuple('Worker', 'function timeout')
        workerlist = [Worker(self.worker1, 10),
                      Worker(self.worker2, 20),
                      Worker(self.worker3, 30), ]
        plist = []
        alive = Value('b', False)
        alive.value = True
        for worker in workerlist:
            p = Process(target=worker.function, args=(alive,s,return_dict))
            plist.append((p,worker.timeout))
            p.start()
        print("%s %s sec" % (sys._getframe().f_code.co_name, (time.time() - s)))

        for p,timeout in plist:
            p.join(timeout)
        #注意位置
        alive.value = False
        print(return_dict)

if __name__ == '__main__':
    # Worker =  namedtuple('Worker','function timeout')
    # workerlist = [Worker(worker1, 2),
    #             Worker(worker2, 2),
    #             Worker(worker3, 3),]
    s = time.time()
    T().process()
    print("end %s sec" % (time.time() - s))    

  

  

posted @ 2020-03-15 00:13  Andrew.SH  阅读(119)  评论(0编辑  收藏  举报