multiprocessing in python jump over process with no error

本小妞迷上赌 提交于 2019-12-12 02:16:41

问题


I have the following code which acts pretty strange.

class A:
    def __init__(self):
        self.lock = Lock()
        self.process_list = []
        self.event_list = []


    def run(self):
        self.process_list = []
        counter = 0
        n = 0
        while (n<1000):
            n += 1
            print("in while again")
            self.lock.acquire()
            print('after acquired lock')
            self.lock.release()
            self.event_list.append(Event())
            print('add event')
            p = Process(target=workerEmulator().run,
                        args=(self.lock, self.event_list[counter]))
            print('create process')
            self.process_list.append(p)
            print('add process')
            self.event_list[counter].clear()
            counter += 1
            print("about to start process")
            print("n="+str(n))
            p.start()
            print("new process started")
            print(": main process, active_children: " + str(multiprocessing.active_children()))


            del_list = []     # the list for storing index of the process/event to delete
            for i in range(len(self.event_list)):
                if (self.event_list[i].is_set()):
                    self.process_list[i].join()
                    print("one process finished, its exit code: " + str(self.process_list[i].exitcode))
                    print("this process is alived or not? " + str(self.process_list[i].is_alive()))

                    del_list.append(i)

            del_list.sort(reverse=True)
            for i in del_list:
                del self.event_list[i]
                del self.process_list[i]
                counter -= 1

            time.sleep(0.1)

        for p in self.process_list:
            p.join()
            print("one process finished, its exit code: " + str(p.exitcode))
            print("this process is alived or not? " + str(p.is_alive()))



class workerEmulator:
    def __init__(self):
        pass

    def run(self, lock, event):
        print("a new process")
        self.lock = lock
        self.event = event

        time.sleep(20)
        print("after sleep")
        self.lock.acquire()
        print("in lock")
        self.lock.release()
        self.event.set()



if __name__ == '__main__':
    a = A()
    a.run()

As I thought, every print statement should be executed 1000 times. However, it is never the case, there are always a few times missing, then if we look at n, I noticed that n will jump over some number, for example, ... n=798...n=799...n=801...n=802... (n=800 missing). I don't see why this happens, could someone help?


回答1:


See Matt's comment, the problem is caused by child output intermingles with parent output.



来源:https://stackoverflow.com/questions/35989545/multiprocessing-in-python-jump-over-process-with-no-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!