Python3.7 pool.apply_async似乎对我不起作用?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (1835)

所以我试图同时运行6个进程,作为测试(我有一个128核CPU,所以目标是并行127个进程),在每个进程中我将运行256个线程来完成一些任务。

我认为我接到电话是pool.apply_async错误的,因此一旦电话通过就似乎没有任何事情发生。我遵循https://docs.python.org/3/library/multiprocessing.html#using-a-pool-of-workers中显示的示例,我不明白我犯了什么错误。

这是执行异步调用的代码段

batch_no = 0
ra = []
for worker_ip in worker_ip_list:
    logg.log("debug","attempting to do async process invocation for workload batch ="+str(batch_no))
    r = worker_pool.apply_async(self.run_worker_for_multi_task,(target_function,worker_ip,threads_per_worker,))
    ra.append(r)
    try:
        logg.log("debug","work pool async call ready status ="+str(r.successful()))
    except Exception:
        logg.log_stacktrace()
    batch_no = batch_no + 1

开头self.run_worker_for_multi_task有一些日志语句,但我没有看到它们中的任何一个被执行。

这是方法的开始。

    def run_worker_for_multi_task(self,tf,worker_ip_list,thread_batch_size):
        l = self.logger.log
        worker_output = Queue()
        l("info","started worker process with PID="+str(os.getpid()))
        l("info","thread batch size is = "+str(thread_batch_size))
        l("debug","creating thread batches...")
...

但这是我得到的输出。

Thu Oct 18 15:38:22 2018 -- INFO -- [directory watcher] directory watching running a scan cycle.
Thu Oct 18 15:38:23 2018 -- DEBUG -- Process Tracker Initialized
Thu Oct 18 15:38:23 2018 -- DEBUG -- [process tracker] {'app_pid': 36935}
Thu Oct 18 15:38:23 2018 -- INFO -- number of workers set to 6
Thu Oct 18 15:38:23 2018 -- INFO -- number of threads per worker set to 256
Thu Oct 18 15:38:23 2018 -- DEBUG -- workload size is - 134208
Thu Oct 18 15:38:23 2018 -- DEBUG -- workload size per worker is going to be - 22368
Thu Oct 18 15:38:23 2018 -- DEBUG -- attempting to do async process invocation for workload batch =0
Thu Oct 18 15:38:23 2018 -- STACK -- Traceback (most recent call last):\n  File "/Users/anupam/PycharmProjects/MultimediaLibrary/core/TaskTracker.py", line 63, in multi_task\n    logg.log("debug","work pool async call ready status ="+str(r.successful()))\n  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/pool.py", line 644, in successful\n    raise ValueError("{0!r} not ready".format(self))\nValueError: <multiprocessing.pool.ApplyResult object at 0x10cb7bda0> not ready\n
Thu Oct 18 15:38:23 2018 -- DEBUG -- attempting to do async process invocation for workload batch =1
Thu Oct 18 15:38:23 2018 -- STACK -- Traceback (most recent call last):\n  File "/Users/anupam/PycharmProjects/MultimediaLibrary/core/TaskTracker.py", line 63, in multi_task\n    logg.log("debug","work pool async call ready status ="+str(r.successful()))\n  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/pool.py", line 644, in successful\n    raise ValueError("{0!r} not ready".format(self))\nValueError: <multiprocessing.pool.ApplyResult object at 0x10cb7bdd8> not ready\n
Thu Oct 18 15:38:23 2018 -- DEBUG -- attempting to do async process invocation for workload batch =2
Thu Oct 18 15:38:23 2018 -- STACK -- Traceback (most recent call last):\n  File "/Users/anupam/PycharmProjects/MultimediaLibrary/core/TaskTracker.py", line 63, in multi_task\n    logg.log("debug","work pool async call ready status ="+str(r.successful()))\n  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/pool.py", line 644, in successful\n    raise ValueError("{0!r} not ready".format(self))\nValueError: <multiprocessing.pool.ApplyResult object at 0x10cb7be48> not ready\n
Thu Oct 18 15:38:23 2018 -- DEBUG -- attempting to do async process invocation for workload batch =3
Thu Oct 18 15:38:23 2018 -- STACK -- Traceback (most recent call last):\n  File "/Users/anupam/PycharmProjects/MultimediaLibrary/core/TaskTracker.py", line 63, in multi_task\n    logg.log("debug","work pool async call ready status ="+str(r.successful()))\n  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/pool.py", line 644, in successful\n    raise ValueError("{0!r} not ready".format(self))\nValueError: <multiprocessing.pool.ApplyResult object at 0x10cb8b6a0> not ready\n
Thu Oct 18 15:38:23 2018 -- DEBUG -- attempting to do async process invocation for workload batch =4
Thu Oct 18 15:38:23 2018 -- STACK -- Traceback (most recent call last):\n  File "/Users/anupam/PycharmProjects/MultimediaLibrary/core/TaskTracker.py", line 63, in multi_task\n    logg.log("debug","work pool async call ready status ="+str(r.successful()))\n  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/pool.py", line 644, in successful\n    raise ValueError("{0!r} not ready".format(self))\nValueError: <multiprocessing.pool.ApplyResult object at 0x10cb8b710> not ready\n
Thu Oct 18 15:38:23 2018 -- DEBUG -- attempting to do async process invocation for workload batch =5
Thu Oct 18 15:38:23 2018 -- STACK -- Traceback (most recent call last):\n  File "/Users/anupam/PycharmProjects/MultimediaLibrary/core/TaskTracker.py", line 63, in multi_task\n    logg.log("debug","work pool async call ready status ="+str(r.successful()))\n  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/pool.py", line 644, in successful\n    raise ValueError("{0!r} not ready".format(self))\nValueError: <multiprocessing.pool.ApplyResult object at 0x10cb8b7b8> not ready\n
Thu Oct 18 15:38:23 2018 -- DEBUG -- all workers completed. shared output data returned by all workers is --
Thu Oct 18 15:38:23 2018 -- DEBUG -- {}

同时我ps -ef | grep -i python在连续循环中运行命令但是在运行代码时我没有看到python进程有任何增加。

而且我知道这self.run_worker_for_multi_task很有效,因为当我通过调用调用它时,我能够从中获得预期的行为Process.start()。问题Process.start()是它阻止并阻止其他进程启动直到进程加入。

即,下面的代码不会运行它并行的进程列表。它在第一次process.start()通话时被阻止

logg.log("debug","creating workers...")
for worker_ip in worker_ip_list:
    worker_inst = Process(target=self.__run_worker_for_multi_task,args=(target_function,worker_ip,q,threads_per_worker,))
    worker_list.append(worker_inst)
logg.log("debug","workers created.")
logg.log("debug","starting workers.")
for worker_inst in worker_list:
    worker_inst.start()
    logg.log("info","starting worker "+str(worker_inst) +" with pid="+str(worker_inst.pid))
logg.log("debug","workers are started")
logg.log("debug","waiting for all workers to complete their tasks")
for worker_inst in worker_list:
    worker_inst.join()

我在这里想念的是什么?为什么我看不到调用六个进程并查看目标的日志语句?如何在多个进程中并行运行该函数?

提问于
用户回答回答于

所以我仍然不知道游泳池的Async会发生什么。但是,我想为什么进程被阻止而且Process.start()无法正常工作。目标必须在公共范围内,否则上下文不能与其他进程共享。因此,将我的目标更改为公共方法就可以了。

扫码关注云+社区

领取腾讯云代金券