微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

解决:使用 ProcessPoolExecutor submit 时子进程不运行 代码:解决方案:

如何解决解决:使用 ProcessPoolExecutor submit 时子进程不运行 代码:解决方案:

我正在尝试使用 while True 异步运行 2 个具有 ProcesspoolExecutor 循环(阻塞)的函数。这两个函数访问同一个共享数组,它的类型是 multiprocessing.Array。数组 (movement) 位于其自己的名为 .pysmglobals.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,))
        # 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 在子进程中运行时包含在函数中,则将无法正常运行。 查看this post的答案。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。