[英]How to run Tornado IO Loop during Behave environment setup
In a project I'm working in I need to cover a Tornado service with Behave so I want to start an instance of my tornado service before running each scenario. 在我正在工作的项目中,我需要使用Behave来覆盖Tornado服务,所以我想在运行每个场景之前启动龙卷风服务的实例。
Naively trying to run the loop as part before all seems to lock the excecution: 天真地尝试将循环作为一部分运行之前似乎锁定了执行:
from tornado import ioloop
from tornadoadapter.applications import APPLICATION
def before_all(context):
print "Service running on port 8000"
APPLICATION.listen(8000)
ioloop.IOLoop.instance().start()
So it's probably not what I need. 所以它可能不是我需要的。
Your IOLoop is running in the main thread, so it's blocking. 您的IOLoop正在主线程中运行,因此它正在阻塞。 You could do it in a separate thread or process. 您可以在单独的线程或进程中执行此操作。
from multiprocessing import Process
from tornado import ioloop
from tornadoadapter.applications import APPLICATION
def run_server():
print "Service running on port 8000"
APPLICATION.listen(8000)
ioloop.IOLoop.instance().start()
def before_all(context):
context.server_thread = Process(target=run_server)
context.server_thread.deamon = True
context.server_thread.start()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.