def execute_shell(self, command): ''' 异步执行外部shell命令 @gen.coroutine def digest(self, message): result = yield self.execute_shell('sleep 1;echo hello') print result.returncode print result.output ''' process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) pipe = process.stdout shell_result = ObjectDict() def handle_events(shell_result, io_loop, fd, events): ''' ioloop异步回调 ''' io_loop.remove_handler(fd) output = ''.join(pipe) process.wait() shell_result['returncode'] = process.returncode shell_result['output'] = output event.set() event = toro.Event() self.io_loop.add_handler( pipe.fileno(), partial(handle_events, shell_result, self.io_loop), self.io_loop.READ) yield event.wait() raise gen.Return(shell_result)
def test_str(self): event = toro.Event() self.assertTrue('clear' in str(event)) self.assertFalse('set' in str(event)) event.set() self.assertFalse('clear' in str(event)) self.assertTrue('set' in str(event))
def _test_event(self, n): e = toro.Event() futures = [] for i in range(n): futures.append(e.wait()) e.set() e.clear() yield futures
def __init__(self, url_table, processor, statistics, concurrent=1): self._url_table = url_table self._processor = processor self._statistics = statistics self._worker_semaphore = AdjustableSemaphore(concurrent) self._done_event = toro.Event() self._num_worker_busy = 0 self._exit_code = 0 self._stopping = False self.stop_event = wpull.actor.Event()
def test_event_timeout(self): e = toro.Event() st = time.time() with assert_raises(toro.Timeout): yield e.wait(deadline=timedelta(seconds=0.1)) duration = time.time() - st self.assertAlmostEqual(0.1, duration, places=1) # After a timed-out waiter, normal operation works st = time.time() self.io_loop.add_timeout(st + 0.1, e.set) result = yield e.wait(deadline=timedelta(seconds=1)) duration = time.time() - st self.assertAlmostEqual(0.1, duration, places=1) self.assertEqual(None, result)
def test_event_nowait(self): e = toro.Event() e.set() self.assertEqual(True, e.is_set()) yield e.wait()
def __init__(self): self.event = toro.Event() self.type = self.body = None
def __init__(self, application, request, **kwargs): super(BaseHandler, self).__init__(application, request, **kwargs) self._server_version = self.settings['server_version'] self._connection_close_event = toro.Event()
def __init__(self, application, request, **kwargs): super(BaseHandler, self).__init__(application, request, **kwargs) self._close_event = toro.Event()
def __init__(self, user, board): super(Game.Player, self).__init__(board) self.user = user self.ready = toro.Event() self.moved = None