Permalink
Browse files

Fixed for IOLoop.start and close

- Removed unused run_loop argument from start
- Use the more efficient Loop.run on start
- Close file descriptors on close
- Removed unused all_handlers argument
  • Loading branch information...
1 parent 8eedb66 commit 155b5e5737992ed75a89c75409541219e13a2f76 @saghul saghul committed with Mar 19, 2013
View
@@ -165,7 +165,7 @@ def stop(self):
self.clients = {}
# finally close the tornado loop
- self.io_loop.close(True)
+ self.io_loop.close()
def restart(self):
# stop the server
@@ -53,7 +53,7 @@ def close(self):
"""Closes the HTTPClient, freeing any resources used."""
if not self._closed:
self._async_client.close()
- self._io_loop.close(True)
+ self._io_loop.close()
self._closed = True
def fetch(self, request, **kwargs):
@@ -185,7 +185,7 @@ def close(self):
def _terminate(self):
self.client_terminated = True
self.stream.close()
- self.stream.io_loop.close(True)
+ self.stream.io_loop.close()
def _write_frame(self, fin, opcode, data):
self.stream.write(frame(data, opcode))
View
@@ -129,7 +129,7 @@ def run(self):
def stop(self):
self.hserver.stop()
- self.io_loop.close(True)
+ self.io_loop.close()
self.started = False
def main():
View
@@ -9,6 +9,7 @@
import datetime
import errno
import logging
+import os
import time
try:
@@ -82,19 +83,18 @@ def cb(handle):
handle.close()
self._loop.walk(cb)
- def close(self, all_fds=False, all_handlers=False):
- if all_fds:
- for fd in self._handlers:
- poll, stack = self._handlers[fd]
- if not poll.closed:
- poll.close()
- self._handlers = {}
-
- if all_handlers:
- self._close_loop_handles()
- # Run the loop so the close callbacks are fired and memory is freed
- # It will not block because all handles are closed
- assert not self._loop.run(pyuv.UV_RUN_NOWAIT), "there are pending handles"
+ def close(self):
+ for fd, (poll, callback) in self._handlers.items():
+ try:
+ os.close(fd)
+ except Exception:
+ logging.debug("error closing fd %s", fd, exc_info=True)
+ if not poll.closed:
+ poll.close()
+ self._handlers = {}
+ # Run the loop so the close callbacks are fired and memory is freed
+ self._loop.run(pyuv.UV_RUN_NOWAIT)
+ self._loop = None
def add_handler(self, fd, handler, events):
if fd in self._handlers:
@@ -131,22 +131,28 @@ def set_blocking_log_threshold(self, seconds):
def log_stack(self, signal, frame):
raise NotImplementedError
- def start(self, run_loop=True):
+ def start(self):
if self._stopped:
self._stopped = False
return
self._thread_ident = thread.get_ident()
self._running = True
- if run_loop:
- while self._running:
- # We should use run() here, but we need to have break() for that
- self._loop.run(pyuv.UV_RUN_ONCE)
- # reset the stopped flag so another start/stop pair can be issued
- self._stopped = False
+
+ # This timer will prevent busy-looping in case there is nothing scheduled in the loop
+ timer = pyuv.Timer(self._loop)
+ timer.start(lambda x: None, 3600, 3600)
+
+ self._loop.run(pyuv.UV_RUN_DEFAULT)
+
+ timer.close()
+
+ # reset the stopped flag so another start/stop pair can be issued
+ self._stopped = False
def stop(self):
self._running = False
self._stopped = True
+ self._loop.stop()
self._waker.wake()
def running(self):
View
@@ -338,11 +338,10 @@ def cb(event, message):
t0 = pyuv.Timer(loop)
t0.start(lambda h: client.close(), 0.4, 0.0)
-
def stop(h):
h.close()
server.stop()
- io_loop.close(True)
+ io_loop.close()
t = pyuv.Timer(loop)
t.start(stop, 0.6, 0.0)
View
@@ -66,7 +66,7 @@ def do_stop(handle):
handle.close()
server.stop()
ws.close()
- io_loop.close(True)
+ io_loop.close()
def init(handle):
handle.close()

0 comments on commit 155b5e5

Please sign in to comment.