How to close a thread when using autoreload

68 views
Skip to first unread message

Yarkee

unread,
Sep 28, 2012, 1:30:56 AM9/28/12
to Tornado Web Server
The code frame is as follow:

threading.Thread(target=my_thread_func).start()
http_server = tornado.httpserver.HTTPServer(Application(),
xheaders=True)
http_server.listen(8080)
io_loop=tornado.ioloop.IOLoop.instance()
tornado.autoreload.start(io_loop)
io_loop.start()

As we know, if we use tornado.autoreload.start(), tornado will reload
the code when we modify some code and save it.
Everytime tornado reload, “threading.Thread(target-
my_thread_func).start() ” will be called and it will exists more and
more threads.
How to close the thread when tornado reload in case of more and more
threads?

Ben Darnell

unread,
Sep 28, 2012, 1:28:08 PM9/28/12
to python-...@googlegroups.com
Autoreload uses the exec() system call, which as a side effect will
automatically destroy all threads in the current process.

-Ben

Yarkee

unread,
Sep 28, 2012, 10:17:11 PM9/28/12
to Tornado Web Server
exec() will copy all process context to a new process, which contains
all the fd.
The thread "my_thread_func" as I mentioned above, will creat some
socket fd.
the result of Autoreload is that: thread is closed, but socket fd not.
More and more fd will be reversed but not be closed, and the rev_bufs
of those socket fds are still receiving tcp segment.
It is awful.

Ben Darnell

unread,
Sep 28, 2012, 10:27:02 PM9/28/12
to python-...@googlegroups.com
Yes, this is why in general it's a good idea to use the FD_CLOEXEC
flag. Tornado does this for all socket fds it opens. If you cannot
easily set FD_CLOEXEC on all fds your application uses, you can use
tornado.autoreload.add_reload_hook to close any remaining files
(perhaps using os.closerange(3, MAX_FD). 3 is the first regular file
descriptor; 0, 1, and 2 are stdin, stdout, and stderr)

http://www.tornadoweb.org/documentation/autoreload.html#tornado.autoreload.add_reload_hook

-Ben

Yarkee

unread,
Sep 28, 2012, 10:57:24 PM9/28/12
to Tornado Web Server
Excellent solution. Thks!

On 9月29日, 上午10时27分, Ben Darnell <b...@bendarnell.com> wrote:
> Yes, this is why in general it's a good idea to use the FD_CLOEXEC
> flag.  Tornado does this for all socket fds it opens.  If you cannot
> easily set FD_CLOEXEC on all fds your application uses, you can use
> tornado.autoreload.add_reload_hook to close any remaining files
> (perhaps using os.closerange(3, MAX_FD).  3 is the first regular file
> descriptor; 0, 1, and 2 are stdin, stdout, and stderr)
>
> http://www.tornadoweb.org/documentation/autoreload.html#tornado.autor...
Reply all
Reply to author
Forward
0 new messages