我有python代码(在此处提供),当某些事件(语音中的关键字识别)发生时,需要将消息发送到nodejs代码(未提供。似乎工作正常)。我正在尝试通过nodejs组件侦听的套接字发送该信息。如果将python套接字代码作为单独的代码,则能够使它工作。我看过其他问题,但我不确定我是否理解回答。帮助表示赞赏。谢谢。
def generate_event():
with SocketIO('localhost', 3000, LoggingNamespace) as socketIO:
socketIO.emit('message', 'Test')
socketIO.disconnect()
socketIO._close()
def another_function:
r = sr.Recognizer()
mic = sr.Microphone()
with mic as source:
if some condition:
generate_event()
if __name__ == '__main__':
app = connexion.App(__name__, specification_dir='swagger/')
app.add_api('swagger.yaml', resolver=RestyResolver('api'))
threading.Thread(target=another_function:).start()
app.run(port=int(os.environ.get('PORT', 9090)))
遇到异常:
Exception in thread Thread-1:
Traceback (most recent call last):
File "***\Continuum\anaconda3\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "***\Continuum\anaconda3\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "app.py", line 58, in another_function
generate_event()
File "app.py", line 21, in generate_event
with SocketIO('localhost', 3000, LoggingNamespace) as socketIO:
File "***\Continuum\anaconda3\lib\site-packages\socketIO_client\__init__.py", line 353, in __init__
resource, hurry_interval_in_seconds, **kw)
File "***\Continuum\anaconda3\lib\site-packages\socketIO_client\__init__.py", line 54, in __init__
self._transport
File "***\Continuum\anaconda3\lib\site-packages\socketIO_client\__init__.py", line 62, in _transport
self._engineIO_session = self._get_engineIO_session()
File "***\Continuum\anaconda3\lib\site-packages\socketIO_client\__init__.py", line 76, in _get_engineIO_session
transport.recv_packet())
StopIteration