Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 212
tutuca Avatar asked Oct 21 '22 07:10

tutuca


1 Answers

Your IOLoop is running in the main thread, so it's blocking. 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()
like image 167
Xuan Avatar answered Nov 01 '22 13:11

Xuan