因此,作为一个测试,我试图同时运行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()
的问题在于它阻止并阻止其他进程启动直到该进程加入。
i.e以下代码段不会运行与其并行的进程列表。它在第一次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()
我在这里想念什么?为什么我看不到六个进程被调用并看到来自目标的日志语句?如何在多个进程中并行运行该功能?
答案 0 :(得分:0)
所以我仍然不知道Pool的Async东西是怎么回事。但是,我知道了为什么进程被阻塞而Process.start()
无法正常工作。目标必须在公共范围内,否则上下文不能与其他进程共享。因此,将目标更改为公共方法就可以解决问题。