python - 已解决:使用 ProcessPoolExecutor 提交时子进程不运行
问题描述
我正在尝试while True
使用ProcessPoolExecutor
. 两个函数都访问同一个共享数组,它的类型是multiprocessing.Array
. 数组 ( movement
) 位于其自己的.py
名为 的文件smglobals.py
中,而takeInput
函数位于另一个 python 文件中,该文件与此代码位于同一目录中:
编码:
# multiprocessing
from concurrent.futures import ProcessPoolExecutor
from multiprocessing import process, shared_memory, Lock
from multiprocessing import Process, Array, Pool
import multiprocessing
from video_input import takeInput
from smglobals import movement
import numpy as np
# misc
import keyboard
from termcolor import colored
def printMovement(movement : Array):
# print(colored(f'{name}: from game', 'blue'))
# existing_shm = shared_memory.SharedMemory(name= name)
# movement = np.ndarray((3,), dtype= np.uint8, buffer= existing_shm.buf)
print(f'movement = {movement[:]}, type = {type(movement[0])}')
while True:
for i in range(len(movement)):
if movement[i] != 0:
print(movement[:])
if keyboard.is_pressed('q'):
print(f'exiting')
break
def initMovement():
import smglobals
smglobals.movement = Array('i', 3)
for i in range(len(smglobals.movement)):
smglobals.movement[i] = 0
if __name__ == '__main__':
initMovement()
with ProcessPoolExecutor(max_workers=2) as executor:
executor.submit(takeInput, (movement, ))
executor.submit(printMovement, (movement, ))
# just for testing
print('hi guy')
这两个函数print
在它们的第一行(在无限循环之前)都有一行,并且它们都不会触发。hi guy
打印,而他们没有打印。
编辑
该takeInput
函数用于通过模型预测tensorflow-gpu
来更改movement
数组。也许这与问题有关?完整的输出是:
2021-05-10 12:21:54.919854: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
hi guy
2021-05-10 12:21:58.169961: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
2021-05-10 12:21:58.169971: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
停在装载区
解决方案:
问题是我包含的方式tensorflow
。
tensorflow
如果在子进程中运行时包含在函数中,将无法正常运行。查看这篇文章的答案。
解决方案
推荐阅读
- optimization - 在多项式上拟合具有不等式约束的多变量多项式
- python - 为什么 `kivy` 被视为一个变量而不是一个模块?
- python - 以编程方式将转义字符添加到 Python 列表
- java - 我们如何使用休眠更新实体的所有引用?
- python - 在 Python/PySpark 中找不到模块“preproc”
- javascript - 为什么我的 else if 语句不起作用?
- javascript - 按 id 在 HTML 文本区域中显示消息
- dart - Dart - 使长时间运行的同步函数异步
- java - Java 中的事件发射器
- css - 在 CSS 中以文本形状剪切图像会导致按钮文本被隐藏