在docker中创建新线程以运行event_loop.run_forever()主线程错误而不会引发异常

时间:2019-04-16 03:17:17

标签: python docker python-asyncio

在docker环境中,我运行了一个关于event_loop.run_forever()的python程序,Docker不打印异常。 我的测试代码:

docker_test.py

import asyncio
import logging
from threading import Thread


logging.basicConfig(level=logging.INFO, format='[%(asctime)s] - %(levelname)s in %(filename)s: %(message)s')
logger = logging.getLogger(__name__)


def start_loop(event_loop):
    """start run_forever"""
    asyncio.set_event_loop(event_loop)
    event_loop.run_forever()


def get_event_loop():
    """new and return event_loop"""
    event_loop = asyncio.new_event_loop()
    t0 = Thread(target=start_loop, args=(event_loop,))
    t0.start()
    return event_loop


if __name__ == '__main__':
    loop = get_event_loop()

    logger.info('make error')
    raise TimeoutError

"""
# if run local
[2019-04-16 11:24:33,044] - INFO in docker_test.py: make error
Traceback (most recent call last):
  File "D:/Code/python/concurrent_crawler/test/docker_test/docker_test.py", line 38, in <module>
    raise TimeoutError
TimeoutError
"""

Dockerfile:

FROM python:3.6
ENV PATH /usr/local/bin:$PATH
ADD . /code
WORKDIR /code
# run
ENTRYPOINT ["python", "docker_test.py"]
CMD [""]

Docker日志如下:

root@myb-ThinkPad-T430:/home/myb/twh/woca_docker# docker run docker_test
[2019-04-16 03:26:32,241] - INFO in docker_test.py: make error

0 个答案:

没有答案