[tornado]Can I use multiple instances of IOLoop in single process?

2,864 views
Skip to first unread message

anonymous

unread,
Oct 10, 2012, 10:51:08 PM10/10/12
to python-...@googlegroups.com
hi all,

I'm getting a TCPClient as well as an UDPServer work together in single process different threads.

I want each of them has a IOLoop, but I can't get it right.

if I use ioloop.IOLoop.instance() both the Client and Server it works, but I treated them as different instance, error occurs sometimes.

When I changed ioloop.IOLoop.instance() into ioloop.IOLoop() just to ensure the instance of IOLoop in Client and Server are different, it just don't work.

I read the code in ioloop.py, the instance method calls IOLoop._instance = IOLoop() when there aren't a global one.

I don't know why, is there something basic I don't know?

Thanks.

Daniel Dotsenko

unread,
Oct 11, 2012, 1:11:21 AM10/11/12
to python-...@googlegroups.com
I have set up a "web application factory" for myself for exactly that case - wanted to run completely separate web application (different handlers, different ports, separate IOLoops).

You need to tippy-toe around how you init/start an instance of IOLoop and in which thread. It uses threading.local and if two of them meet in the same thread you have issues. You must loop.start() inside the sub-thread.

Example (the __name__ == "__main__" is pseudo-code - written off-hand without testing):


Daniel

Andrew Grigorev

unread,
Oct 11, 2012, 6:56:34 AM10/11/12
to python-...@googlegroups.com, anonymous
Hi.

You have to make a several IOLoop instances, one per thread, using
IOLoop(). IOLoop.instance() could be used to simplify you code if there
is only one thread in your program, or if the other threads are just
communicating with the main thread through ioloop.add_callback method.
See
http://www.tornadoweb.org/documentation/ioloop.html#running-an-ioloop
for common IOLoop usage pattern.

11.10.2012 06:51, anonymous пишет:
--
Andrew

anonymous

unread,
Oct 11, 2012, 8:44:01 PM10/11/12
to python-...@googlegroups.com
I've figured it out, I use IOStream to build the TCP Client and I didn't pass the ioloop instance to while creating the IOStream, so it created one inside itself, by calling ioloop.IOLoop.instance(), and that caused the problem.

Thank you!

2012/10/11 Daniel Dotsenko <dvdot...@gmail.com>
Reply all
Reply to author
Forward
0 new messages