【发布时间】:2018-07-20 04:42:34
【问题描述】:
我设计了一段 python 代码,它本质上是在更大的方案中作为微服务工作的。
我在循环中安排了两个任务,我设置了两个任务在执行程序中运行。
奇怪的是代码运行良好。做我期望的一切。但是当我用 KeyboardInterrupt (Ctrl+C) 结束它时,我会看到错误和异常。这让我觉得我肯定在这里滥用了 asyncio 模式。我将尝试提供代码的简要概述,而不会立即进入冗长的细节:
class Prototype:
def _redis_subscriber(self):
self._p = self._redis_itx.pubsub(ignore_subscribe_messages=True)
self._p.subscribe("channel1")
while True:
pubbed_msg = self._p.get_message()
if pubbed_msg is not None:
#process process process
time.sleep(0.01)
def _generic_worker_on_internal_q(self):
while True:
item = self.q.get() #blocking call
#process item
async def task1(self):
#network I/O bound code
async def task2(self):
#network I/O bound code. also fills with self.q.put()
def run(self):
asyncio.ensure_future(self.task1(), loop=self._event_loop)
asyncio.ensure_future(self.task2(), loop=self._event_loop)
asyncio.ensure_future(self._event_loop.run_in_executor(None, self._redis_subscriber))
asyncio.ensure_future(self._event_loop.run_in_executor(None, self._generic_worker_on_internal_q))
self._event_loop.run_forever()
if __name__ == '__main__':
p = Prototype()
p.run()
另外,我尝试在 Protoype.run() 方法中尝试另一种方法:
def __init__(self):
self._tasks = []
def run(self):
self._tasks.append(asyncio.ensure_future(self._task1()))
self._tasks.append(asyncio.ensure_future(self._task2()))
self._tasks.append(asyncio.ensure_future(self._event_loop.run_in_executor(None, self._redis_subscriber)))
self._tasks.append(asyncio.ensure_future(self._event_loop.run_in_executor(None, self._generic_worker_on_internal_q)))
self._event_loop.run_until_complete(self._tasks)
无论如何,当我尝试使用 Ctrl+C 结束正在运行的脚本时,它不会在第一次尝试时退出。我必须按两次。这就是出现的情况:
KeyboardInterrupt
^CError in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/concurrent/futures/thread.py", line 40, in _python_exit
t.join()
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 1056, in join
self._wait_for_tstate_lock()
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 1072, in _wait_for_tstate_lock
elif lock.acquire(block, timeout):
KeyboardInterrupt
Exception ignored in: <bound method BaseEventLoop.call_exception_handler of <_UnixSelectorEventLoop running=False closed=False debug=False>>
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py", line 1296, in call_exception_handler
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 1335, in error
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 1442, in _log
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 1452, in handle
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 1514, in callHandlers
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 863, in handle
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 1069, in emit
File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 1059, in _open
NameError: name 'open' is not defined
我哪里错了?
【问题讨论】:
-
也许可以在启用调试的情况下尝试相同的操作。 docs.python.org/3/library/asyncio-dev.html ?
-
您的一个
run_in_executor线程未在中断时退出; atexit 处理程序将None发送到工作队列,然后加入它们。None通知这些线程退出,但正在运行的函数永远不会退出,因此工作线程永远不会退出。
标签: python multithreading asynchronous redis event-loop