我正在运行一个Python 3.7 Flask应用程序,该应用程序使用flask_socketio
为浏览器客户端设置socketio
服务器,另一个python进程连接到单独的远程socketio
服务器并交换消息,另一个python进程从PIR传感器读取输入。
两个python进程都通过multiprocessing.Queue
进行通信 - 但是,socketio
进程总是[WinError6] - Invalid Handle
或[WinError5] - Permission Denied
。我完全不知道我做错了什么。
这是顶级(服务器(代码;它似乎没有问题:
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
...
以上工作正常且花花公子。以下块中的倒数第二行是问题所在。
# 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
做到了。