已解决:使用ProcessPoolExecutor submit时子进程不运行

2024-06-28 11:53:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用ProcessPoolExecutor异步运行两个具有while True循环(阻塞)的函数。这两个函数访问相同的共享数组,其类型为multiprocessing.Array。数组(movement)位于它自己的名为smglobals.py.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

在装货区停车

解决方案:


问题是我加入的方式。 ^如果在子进程中运行时包含在函数中,{}将无法正常运行。 查看this post的答案


Tags: 文件函数namefromimporttensorflow数组multiprocessing