python - WinError6 句柄无效 Python 3+ 多处理

标签 python windows multiprocessing

我正在运行一个 Python 3.7 Flask 应用程序,它使用 flask_socketio 为浏览器客户端设置一个 socketio 服务器,另一个 python 进程连接到一个单独的远程 socketio 服务器和交换消息,以及另一个从 PIR 传感器读取输入的 python 进程。

两个 python 进程通过 multiprocessing.Queue 进行通信 - 但是,socketio 进程总是得到 [WinError6] - Invalid Handle[WinError5] - 权限被拒绝。我完全不知道自己做错了什么。

这是顶级(服务器)代码; 它似乎没有问题:

from shotsocket import init as shotsocket_init
from shotsocket import util as matchmaking_util
import multiprocessing, os, config, uuid

match_queue = multiprocessing.Queue()
shot_queue = multiprocessing.Queue()

app = Flask(__name__, static_url_path='', static_folder='templates')
socketio = SocketIO(app)

_rooms = [] # I don't plan to keep this in memory, just doing it for debug / dev 

...

以上工作正常而且花花公子。以下 block 中的倒数第二行是问题所在。

# THIS IS THE FUNC WHERE WE ARE TRYING TO USE 
# THE BROKEN QUEUE

@socketio.on('connect')
def listen():
    room_key = str(uuid.uuid4())
    join_room(room_key)
    _rooms.append((room_key, request.sid))
    possible_match = matchmaking_util.match_pending_clients(_rooms)
    if possible_match:
        shot_queue.put_nowait(possible_match)
        print('put it in there')

以下是我如何开始这些过程:


if __name__ == '__main__':
    debug = os.environ.get('MOONSHOT_DEBUG', False)
    try:
        proc = multiprocessing.Process(target=start, args=(debug,match_queue))
        proc.start()
        shot_proc = multiprocessing.Process(target=shotsocket_init, args=(shot_queue,))
        shot_proc.start()
        socketio.run(app, host='0.0.0.0')
    except KeyboardInterrupt:
        socketio.stop()
        proc.join()
        shot_proc.join()

下面是整个shotsocket(无法读取队列的代码)

import socketio, multiprocessing # mp for the type

sio = socketio.Client(engineio_logger=True)
sio.connect('redacted woot', transports=['websocket'])


@sio.on('connect')
def connect():
    print("connected to shot server")

def init(queue: multiprocessing.Queue):
    while True:
        try:
            # WE NEVER GET PAST THIS LINE
            print(queue.get())
        except Exception as e:
            continue
        if not queue.empty():
            print('queue empty')
            shot = queue.get()
            print(shot)         
            match_id, opponents = shot
            sio.emit('start', {'id': match_id, 'opponents': [opponents[0], opponents[1]]})

我正在拔头发。我到底做错了什么?

最佳答案

解决方案

我不知道为什么这可以解决问题,但是从 multiprocessing.Queue 切换到 queue.Queue 并将 multiprocessing.Process 切换到 threading.Thread 做到了。

关于python - WinError6 句柄无效 Python 3+ 多处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54639506/

相关文章:

windows - 与 PhoneGap 紧密相关的桌面发布平台?

c++ - Windows - 在 C++ 中读取鼠标的 dpi 设置

python - 并行更新python中的搁置字典

python - 如何根据本地位置/旋转在 OpenGL 中移动/旋转对象

python - 如何删除 Python 类中隐式传递的 self ?

python - HTTP 错误 401 : Unauthorized using urllib. request.urlopen

Python 多处理 - 从 3 个不同的函数返回值

python - 从排序列表中提取索引

c++ - 取消 DeferWindowPos

python - 如何在 Python 中使用多处理对循环进行并行求和