def fork_main(parent_conn, child_conn): parent_conn.close() loop = asyncio._wrap_loop() # This fails with python's default event loop policy, # see https://bugs.python.org/issue22087. loop.run_until_complete(asyncio.sleep(0.1, loop=loop)) loop.close()
def testAsyncCancel(self): loop = global_event_loop() input_futures = set() future_count = 3 def future_generator(): for i in range(future_count): future = loop.create_future() loop.call_soon( lambda future: None if future.done() else future.set_result(None), future) input_futures.add(future) yield future for future_done_set in async_iter_completed(future_generator(), max_jobs=True, max_load=True, loop=loop): while not input_futures: loop.run_until_complete(asyncio.sleep(0, loop=loop)) future_done_set.cancel() break # With max_jobs=True, async_iter_completed should have executed # the generator until it raised StopIteration. self.assertEqual(future_count, len(input_futures)) loop.run_until_complete(asyncio.wait(input_futures, loop=loop)) # The futures may have results or they may have been cancelled # by TaskScheduler, and behavior varies depending on the python # interpreter. for future in input_futures: future.cancelled() or future.result()
def _do_test(self, read_end, write_end): initial_policy = asyncio.get_event_loop_policy() if not isinstance(initial_policy, DefaultEventLoopPolicy): asyncio.set_event_loop_policy(DefaultEventLoopPolicy()) loop = asyncio._wrap_loop() read_end = os.fdopen(read_end, 'rb', 0) write_end = os.fdopen(write_end, 'wb', 0) try: def reader_callback(): if not reader_callback.called.done(): reader_callback.called.set_result(None) reader_callback.called = loop.create_future() loop.add_reader(read_end.fileno(), reader_callback) # Allow the loop to check for IO events, and assert # that our future is still not done. loop.run_until_complete(asyncio.sleep(0, loop=loop)) self.assertFalse(reader_callback.called.done()) # Demonstrate that the callback is called afer the # other end of the pipe has been closed. write_end.close() loop.run_until_complete(reader_callback.called) finally: loop.remove_reader(read_end.fileno()) write_end.close() read_end.close() asyncio.set_event_loop_policy(initial_policy) if loop not in (None, global_event_loop()): loop.close() self.assertFalse(global_event_loop().is_closed())
def ready(self): """ Wait for the proxy socket to become ready. This method is a coroutine. """ while True: try: wait_retval = os.waitpid(self._pids[0], os.WNOHANG) except OSError as e: if e.errno == errno.EINTR: continue raise if wait_retval is not None and wait_retval != (0, 0): raise OSError(3, 'No such process') try: s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.connect(self.socket_path) except EnvironmentError as e: if e.errno != errno.ENOENT: raise yield asyncio.sleep(0.2) else: break finally: s.close()
def ready(self): """ Wait for the proxy socket to become ready. This method is a coroutine. """ while True: try: wait_retval = os.waitpid(self._pids[0], os.WNOHANG) except OSError as e: if e.errno == errno.EINTR: continue raise if wait_retval is not None and wait_retval != (0, 0): raise OSError(3, 'No such process') try: s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.connect(self.socket_path) except EnvironmentError as e: if e.errno != errno.ENOENT: raise yield asyncio.sleep(0.2) else: break finally: s.close()
def _do_test(self, read_end, write_end): initial_policy = asyncio.get_event_loop_policy() if not isinstance(initial_policy, DefaultEventLoopPolicy): asyncio.set_event_loop_policy(DefaultEventLoopPolicy()) loop = asyncio.get_event_loop() read_end = os.fdopen(read_end, 'rb', 0) write_end = os.fdopen(write_end, 'wb', 0) try: def reader_callback(): if not reader_callback.called.done(): reader_callback.called.set_result(None) reader_callback.called = loop.create_future() loop.add_reader(read_end.fileno(), reader_callback) # Allow the loop to check for IO events, and assert # that our future is still not done. loop.run_until_complete(asyncio.sleep(0, loop=loop)) self.assertFalse(reader_callback.called.done()) # Demonstrate that the callback is called afer the # other end of the pipe has been closed. write_end.close() loop.run_until_complete(reader_callback.called) finally: loop.remove_reader(read_end.fileno()) write_end.close() read_end.close() asyncio.set_event_loop_policy(initial_policy)
def _proc_join(self, proc): sentinel_reader = self.scheduler.create_future() self.scheduler.add_reader( proc.sentinel, lambda: sentinel_reader.done() or sentinel_reader.set_result(None)) try: yield sentinel_reader finally: # If multiprocessing.Process supports the close method, then # access to proc.sentinel will raise ValueError if the # sentinel has been closed. In this case it's not safe to call # remove_reader, since the file descriptor may have been closed # and then reallocated to a concurrent coroutine. When the # close method is not supported, proc.sentinel remains open # until proc's finalizer is called. try: self.scheduler.remove_reader(proc.sentinel) except ValueError: pass # Now that proc.sentinel is ready, poll until process exit # status has become available. while True: proc.join(0) if proc.exitcode is not None: break yield asyncio.sleep(self._proc_join_interval)
def fork_main(parent_conn, child_conn): parent_conn.close() loop = asyncio._wrap_loop() # This fails with python's default event loop policy, # see https://bugs.python.org/issue22087. loop.run_until_complete(asyncio.sleep(0.1, loop=loop)) loop.close()
def _do_test(self, read_end, write_end): initial_policy = asyncio.get_event_loop_policy() if not isinstance(initial_policy, DefaultEventLoopPolicy): asyncio.set_event_loop_policy(DefaultEventLoopPolicy()) loop = asyncio._wrap_loop() read_end = os.fdopen(read_end, 'rb', 0) write_end = os.fdopen(write_end, 'wb', 0) try: def writer_callback(): if not writer_callback.called.done(): writer_callback.called.set_result(None) writer_callback.called = loop.create_future() _set_nonblocking(write_end.fileno()) loop.add_writer(write_end.fileno(), writer_callback) # With pypy we've seen intermittent spurious writer callbacks # here, so retry until the correct state is achieved. tries = 10 while tries: tries -= 1 # Fill up the pipe, so that no writer callbacks should be # received until the state has changed. while True: try: os.write(write_end.fileno(), 512 * b'0') except EnvironmentError as e: if e.errno != errno.EAGAIN: raise break # Allow the loop to check for IO events, and assert # that our future is still not done. loop.run_until_complete(asyncio.sleep(0, loop=loop)) if writer_callback.called.done(): writer_callback.called = loop.create_future() else: break self.assertFalse(writer_callback.called.done()) # Demonstrate that the callback is called afer the # other end of the pipe has been closed. read_end.close() loop.run_until_complete(writer_callback.called) finally: loop.remove_writer(write_end.fileno()) write_end.close() read_end.close() asyncio.set_event_loop_policy(initial_policy) if loop not in (None, global_event_loop()): loop.close() self.assertFalse(global_event_loop().is_closed())
def _do_test(self, read_end, write_end): initial_policy = asyncio.get_event_loop_policy() if not isinstance(initial_policy, DefaultEventLoopPolicy): asyncio.set_event_loop_policy(DefaultEventLoopPolicy()) loop = asyncio._wrap_loop() read_end = os.fdopen(read_end, 'rb', 0) write_end = os.fdopen(write_end, 'wb', 0) try: def writer_callback(): if not writer_callback.called.done(): writer_callback.called.set_result(None) writer_callback.called = loop.create_future() _set_nonblocking(write_end.fileno()) loop.add_writer(write_end.fileno(), writer_callback) # With pypy we've seen intermittent spurious writer callbacks # here, so retry until the correct state is achieved. tries = 10 while tries: tries -= 1 # Fill up the pipe, so that no writer callbacks should be # received until the state has changed. while True: try: os.write(write_end.fileno(), 512 * b'0') except EnvironmentError as e: if e.errno != errno.EAGAIN: raise break # Allow the loop to check for IO events, and assert # that our future is still not done. loop.run_until_complete(asyncio.sleep(0, loop=loop)) if writer_callback.called.done(): writer_callback.called = loop.create_future() else: break self.assertFalse(writer_callback.called.done()) # Demonstrate that the callback is called afer the # other end of the pipe has been closed. read_end.close() loop.run_until_complete(writer_callback.called) finally: loop.remove_writer(write_end.fileno()) write_end.close() read_end.close() asyncio.set_event_loop_policy(initial_policy) if loop not in (None, global_event_loop()): loop.close() self.assertFalse(global_event_loop().is_closed())
def wait(self): """ Wait for the queue to become empty. This method is a coroutine. """ while self: task = next(iter(self.running_tasks), None) if task is None: # Wait for self.running_tasks to populate. yield asyncio.sleep(0) else: yield task.async_wait()
def _wait_loop(self, timeout=None): loop = self.scheduler tasks = [self.async_wait()] if timeout is not None: tasks.append(asyncio.ensure_future( asyncio.sleep(timeout, loop=loop), loop=loop)) try: loop.run_until_complete(asyncio.ensure_future( asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED, loop=loop), loop=loop)) finally: for task in tasks: task.cancel()
def _wait_loop(self, timeout=None): loop = self.scheduler tasks = [self.async_wait()] if timeout is not None: tasks.append(asyncio.ensure_future( asyncio.sleep(timeout, loop=loop), loop=loop)) try: loop.run_until_complete(asyncio.ensure_future( asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED, loop=loop), loop=loop)) finally: for task in tasks: task.cancel()
def fetch_wait_result(scheduler, first, loop=None): if first: yield scheduler.async_start() # If the current coroutine awakens just after a call to # done_callback but before scheduler has been notified of # corresponding done future(s), then wait here until scheduler # is notified (which will cause future_map to populate). while not future_map and scheduler.poll() is None: yield asyncio.sleep(0, loop=loop) if not future_map: if scheduler.poll() is not None: coroutine_return((set(), set())) else: raise AssertionError('expected non-empty future_map') wait_result = yield asyncio.wait(list(future_map.values()), return_when=asyncio.FIRST_COMPLETED, loop=loop) coroutine_return(wait_result)
def returning_coroutine(): yield asyncio.sleep(0) coroutine_return('success')
def raising_coroutine(): yield asyncio.sleep(0) raise TestException('exception')
def yield_expression_coroutine(): for i in range(3): x = yield asyncio.sleep(0, result=i) self.assertEqual(x, i)
def raising_coroutine(loop=None): yield asyncio.sleep(0, loop=loop) raise TestException('exception')
def returning_coroutine(loop=None): yield asyncio.sleep(0, loop=loop) coroutine_return('success')
def yield_expression_coroutine(loop=None): for i in range(3): x = yield asyncio.sleep(0, result=i, loop=loop) self.assertEqual(x, i)
def raising_coroutine(): yield asyncio.sleep(0) raise TestException('exception')
def returning_coroutine(): yield asyncio.sleep(0) coroutine_return('success')