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()
Exemplo n.º 2
0
    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()
Exemplo n.º 3
0
	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())
Exemplo n.º 4
0
	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()
Exemplo n.º 5
0
    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()
Exemplo n.º 6
0
    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)
Exemplo n.º 7
0
    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)
Exemplo n.º 8
0
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()
Exemplo n.º 9
0
    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())
Exemplo n.º 10
0
	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())
Exemplo n.º 11
0
    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()
Exemplo n.º 12
0
	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()
Exemplo n.º 13
0
	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()
Exemplo n.º 14
0
    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)
Exemplo n.º 15
0
 def returning_coroutine():
     yield asyncio.sleep(0)
     coroutine_return('success')
Exemplo n.º 16
0
		def raising_coroutine():
			yield asyncio.sleep(0)
			raise TestException('exception')
Exemplo n.º 17
0
		def yield_expression_coroutine():
			for i in range(3):
				x = yield asyncio.sleep(0, result=i)
				self.assertEqual(x, i)
Exemplo n.º 18
0
 def raising_coroutine(loop=None):
     yield asyncio.sleep(0, loop=loop)
     raise TestException('exception')
Exemplo n.º 19
0
 def returning_coroutine(loop=None):
     yield asyncio.sleep(0, loop=loop)
     coroutine_return('success')
Exemplo n.º 20
0
 def yield_expression_coroutine(loop=None):
     for i in range(3):
         x = yield asyncio.sleep(0, result=i, loop=loop)
         self.assertEqual(x, i)
Exemplo n.º 21
0
 def raising_coroutine():
     yield asyncio.sleep(0)
     raise TestException('exception')
Exemplo n.º 22
0
		def returning_coroutine():
			yield asyncio.sleep(0)
			coroutine_return('success')