Example #1
0
 def test_init_constructor_default_loop(self):
     try:
         events.set_event_loop(self.loop)
         f = futures.Future()
         self.assertIs(f._loop, self.loop)
     finally:
         events.set_event_loop(None)
Example #2
0
 def test_ctor_noloop(self):
     try:
         events.set_event_loop(self.loop)
         q = queues.Queue()
         self.assertIs(q._loop, self.loop)
     finally:
         events.set_event_loop(None)
Example #3
0
 def test_ctor_noloop(self):
     try:
         events.set_event_loop(self.loop)
         cond = locks.Condition()
         self.assertIs(cond._loop, self.loop)
     finally:
         events.set_event_loop(None)
Example #4
0
 def test_ctor_noloop(self):
     try:
         events.set_event_loop(self.loop)
         sem = locks.Semaphore()
         self.assertIs(sem._loop, self.loop)
     finally:
         events.set_event_loop(None)
Example #5
0
 def test_ctor_noloop(self):
     try:
         events.set_event_loop(self.loop)
         lock = locks.Lock()
         self.assertIs(lock._loop, self.loop)
     finally:
         events.set_event_loop(None)
Example #6
0
 def test_ctor_noloop(self):
     try:
         events.set_event_loop(self.loop)
         cond = locks.Condition()
         self.assertIs(cond._loop, self.loop)
     finally:
         events.set_event_loop(None)
Example #7
0
    def test_wait_with_global_loop(self):

        def gen():
            when = yield
            self.assertAlmostEqual(0.01, when)
            when = yield 0
            self.assertAlmostEqual(0.015, when)
            yield 0.015

        loop = test_utils.TestLoop(gen)
        self.addCleanup(loop.close)

        a = tasks.Task(tasks.sleep(0.01, loop=loop), loop=loop)
        b = tasks.Task(tasks.sleep(0.015, loop=loop), loop=loop)

        @tasks.coroutine
        def foo():
            done, pending = yield from tasks.wait([b, a])
            self.assertEqual(done, set([a, b]))
            self.assertEqual(pending, set())
            return 42

        events.set_event_loop(loop)
        try:
            res = loop.run_until_complete(
                tasks.Task(foo(), loop=loop))
        finally:
            events.set_event_loop(None)

        self.assertEqual(res, 42)
Example #8
0
    def run(self, debug=False):

        if events._get_running_loop() is not None:
            raise RuntimeError("There is already a running event loop")

        async def runner():
            try:
                await self.start()
            finally:
                await self.close()

        loop = self.loop
        try:
            events.set_event_loop(loop)
            loop.set_debug(debug)
            loop.run_until_complete(runner())
        except KeyboardInterrupt:
            self.logger.info("Received signal to terminate tasks and event loop.")
        finally:
            try:
                self.logger.debug("Cleaning up tasks.")
                _cancel_all_tasks(loop)
                loop.run_until_complete(loop.shutdown_asyncgens())
                # loop.run_until_complete(loop.shutdown_default_executor())
            finally:
                events.set_event_loop(None)
                self.logger.info("Closing event loop.")
Example #9
0
 def test_ctor_noloop(self):
     try:
         events.set_event_loop(self.loop)
         q = queues.Queue()
         self.assertIs(q._loop, self.loop)
     finally:
         events.set_event_loop(None)
Example #10
0
    def test_wait_for_with_global_loop(self):

        def gen():
            when = yield
            self.assertAlmostEqual(0.2, when)
            when = yield 0
            self.assertAlmostEqual(0.01, when)
            yield 0.01

        loop = test_utils.TestLoop(gen)
        self.addCleanup(loop.close)

        @tasks.coroutine
        def foo():
            yield from tasks.sleep(0.2, loop=loop)
            return 'done'

        events.set_event_loop(loop)
        try:
            fut = tasks.Task(foo(), loop=loop)
            with self.assertRaises(futures.TimeoutError):
                loop.run_until_complete(tasks.wait_for(fut, 0.01))
        finally:
            events.set_event_loop(None)

        self.assertAlmostEqual(0.01, loop.time())
        self.assertFalse(fut.done())

        # move forward to close generator
        loop.advance_time(10)
        loop.run_until_complete(fut)
Example #11
0
 def test_ctor_noloop(self):
     try:
         events.set_event_loop(self.loop)
         lock = locks.Lock()
         self.assertIs(lock._loop, self.loop)
     finally:
         events.set_event_loop(None)
Example #12
0
 def test_init_constructor_default_loop(self):
     try:
         events.set_event_loop(self.loop)
         f = futures.Future()
         self.assertIs(f._loop, self.loop)
     finally:
         events.set_event_loop(None)
Example #13
0
 def test_ctor_noloop(self):
     try:
         events.set_event_loop(self.loop)
         ev = locks.Event()
         self.assertIs(ev._loop, self.loop)
     finally:
         events.set_event_loop(None)
Example #14
0
 def test_ctor_noloop(self):
     try:
         events.set_event_loop(self.loop)
         ev = locks.Event()
         self.assertIs(ev._loop, self.loop)
     finally:
         events.set_event_loop(None)
Example #15
0
    def test_wait_for_with_global_loop(self):
        def gen():
            when = yield
            self.assertAlmostEqual(0.2, when)
            when = yield 0
            self.assertAlmostEqual(0.01, when)
            yield 0.01

        loop = test_utils.TestLoop(gen)
        self.addCleanup(loop.close)

        @tasks.coroutine
        def foo():
            yield from tasks.sleep(0.2, loop=loop)
            return 'done'

        events.set_event_loop(loop)
        try:
            fut = tasks.Task(foo(), loop=loop)
            with self.assertRaises(futures.TimeoutError):
                loop.run_until_complete(tasks.wait_for(fut, 0.01))
        finally:
            events.set_event_loop(None)

        self.assertAlmostEqual(0.01, loop.time())
        self.assertFalse(fut.done())

        # move forward to close generator
        loop.advance_time(10)
        loop.run_until_complete(fut)
Example #16
0
    def tearDown(self):
        """Tear down test."""
        if self._exception:
            try:
                self.machine.shutdown()
            except:
                pass

            if self._exception and 'exception' in self._exception:
                raise self._exception['exception']
            elif self._exception:
                raise Exception(self._exception)

        duration = time.time() - self.test_start_time
        if duration > self.expected_duration:
            print("Test {}.{} took {} > {}s".format(self.__class__,
                                                    self._testMethodName,
                                                    round(duration, 2),
                                                    self.expected_duration))

        self.machine.log.debug("Test ended")
        if sys.exc_info != (None, None, None):
            # disable teardown logging after error
            logging.basicConfig(level=99)
        else:
            # fire all delays
            self.advance_time_and_run(300)
        self.machine._do_stop()
        self.machine = None

        self.restore_sys_path()
        events.set_event_loop(None)
Example #17
0
    def test_wait_with_global_loop(self):
        def gen():
            when = yield
            self.assertAlmostEqual(0.01, when)
            when = yield 0
            self.assertAlmostEqual(0.015, when)
            yield 0.015

        loop = test_utils.TestLoop(gen)
        self.addCleanup(loop.close)

        a = tasks.Task(tasks.sleep(0.01, loop=loop), loop=loop)
        b = tasks.Task(tasks.sleep(0.015, loop=loop), loop=loop)

        @tasks.coroutine
        def foo():
            done, pending = yield from tasks.wait([b, a])
            self.assertEqual(done, set([a, b]))
            self.assertEqual(pending, set())
            return 42

        events.set_event_loop(loop)
        try:
            res = loop.run_until_complete(tasks.Task(foo(), loop=loop))
        finally:
            events.set_event_loop(None)

        self.assertEqual(res, 42)
Example #18
0
 def set_event_loop(self, loop, *, cleanup=True):
     if loop is None:
         raise AssertionError('loop is None')
     # ensure that the event loop is passed explicitly in asyncio
     events.set_event_loop(None)
     if cleanup:
         self.addCleanup(self.close_loop, loop)
Example #19
0
 def test_ctor_noloop(self):
     try:
         events.set_event_loop(self.loop)
         sem = locks.Semaphore()
         self.assertIs(sem._loop, self.loop)
     finally:
         events.set_event_loop(None)
Example #20
0
def sitemap_gen(link, link_key):
    if '--iocp' in sys.argv:
        sys.argv.remove('--iocp')
        logging.info('using iocp')
        el = windows_events.ProactorEventLoop()
        events.set_event_loop(el)
    root_url = link
    crawler(root_url, out_file=f'sitemap_{link_key}.xml')
Example #21
0
    def tearDown(self):
        events.set_event_loop(None)

        # Detect CPython bug #23353: ensure that yield/yield-from is not used
        # in an except block of a generator
        self.assertEqual(sys.exc_info(), (None, None, None))

        self.doCleanups()
        threading_helper.threading_cleanup(*self._thread_cleanup)
        support.reap_children()
Example #22
0
    def asyncio_run(future):  # NOQA:WPS440
        """Execute future in loop.

        :param future: some future
        :return: result
        """
        loop = events.new_event_loop()
        future_result = loop.run_until_complete(future)
        events.set_event_loop(None)
        loop.close()
        return future_result  # NOQA:WPS331
Example #23
0
 def _check_empty_sequence(self, seq_or_iter):
     events.set_event_loop(self.one_loop)
     self.addCleanup(events.set_event_loop, None)
     fut = tasks.gather(*seq_or_iter)
     self.assertIsInstance(fut, futures.Future)
     self.assertIs(fut._loop, self.one_loop)
     self._run_loop(self.one_loop)
     self.assertTrue(fut.done())
     self.assertEqual(fut.result(), [])
     fut = tasks.gather(*seq_or_iter, loop=self.other_loop)
     self.assertIs(fut._loop, self.other_loop)
Example #24
0
 def _check_empty_sequence(self, seq_or_iter):
     events.set_event_loop(self.one_loop)
     self.addCleanup(events.set_event_loop, None)
     fut = tasks.gather(*seq_or_iter)
     self.assertIsInstance(fut, futures.Future)
     self.assertIs(fut._loop, self.one_loop)
     self._run_loop(self.one_loop)
     self.assertTrue(fut.done())
     self.assertEqual(fut.result(), [])
     fut = tasks.gather(*seq_or_iter, loop=self.other_loop)
     self.assertIs(fut._loop, self.other_loop)
Example #25
0
def run(main):
    loop = events.new_event_loop()
    try:
        events.set_event_loop(loop)
        return loop.run_until_complete(main)
    finally:
        try:
            loop.run_until_complete(loop.shutdown_asyncgens())
        finally:
            events.set_event_loop(None)
            loop.close()
Example #26
0
    def tearDown(self):
        self.unpatch_get_running_loop()

        events.set_event_loop(None)

        # Detect CPython bug #23353: ensure that yield/yield-from is not used
        # in an except block of a generator
        self.assertEqual(sys.exc_info(), (None, None, None))

        self.doCleanups()
        support.threading_cleanup(*self._thread_cleanup)
        support.reap_children()
Example #27
0
    def setUp(self):
        super(AsyncIOTestCase, self).setUp()

        # Ensure that the event loop is passed explicitly in Motor.
        events.set_event_loop(None)
        self.loop = asyncio.new_event_loop()

        if self.ssl and not env.mongod_started_with_ssl:
            raise SkipTest("mongod doesn't support SSL, or is down")

        self.cx = self.asyncio_client(ssl=self.ssl)
        self.db = self.cx.motor_test
        self.collection = self.db.test_collection
Example #28
0
    def setUp(self):
        super(AsyncIOTestCase, self).setUp()

        # Ensure that the event loop is passed explicitly in Motor.
        events.set_event_loop(None)
        self.loop = asyncio.new_event_loop()

        if self.ssl and not env.mongod_started_with_ssl:
            raise SkipTest("mongod doesn't support SSL, or is down")

        self.cx = self.asyncio_client(ssl=self.ssl)
        self.db = self.cx.motor_test
        self.collection = self.db.test_collection
Example #29
0
    def test_open_connection_no_loop_ssl(self):
        with test_utils.run_test_server(use_ssl=True) as httpd:
            try:
                events.set_event_loop(self.loop)
                f = streams.open_connection(*httpd.address,
                                            ssl=test_utils.dummy_ssl_context())
                reader, writer = self.loop.run_until_complete(f)
            finally:
                events.set_event_loop(None)
            writer.write(b'GET / HTTP/1.0\r\n\r\n')
            f = reader.read()
            data = self.loop.run_until_complete(f)
            self.assertTrue(data.endswith(b'\r\n\r\nTest message'))

            writer.close()
Example #30
0
    def test_open_connection_no_loop_ssl(self):
        with test_utils.run_test_server(use_ssl=True) as httpd:
            try:
                events.set_event_loop(self.loop)
                f = streams.open_connection(*httpd.address,
                                            ssl=test_utils.dummy_ssl_context())
                reader, writer = self.loop.run_until_complete(f)
            finally:
                events.set_event_loop(None)
            writer.write(b'GET / HTTP/1.0\r\n\r\n')
            f = reader.read()
            data = self.loop.run_until_complete(f)
            self.assertTrue(data.endswith(b'\r\n\r\nTest message'))

            writer.close()
Example #31
0
 def run(main, *, debug=False):
     loop = events._get_running_loop()
     if not loop:
         loop = events.new_event_loop()
         events.set_event_loop(loop)
         _patch_loop(loop)
     loop.set_debug(debug)
     task = asyncio.ensure_future(main)
     try:
         return loop.run_until_complete(task)
     finally:
         if not task.done():
             task.cancel()
             with suppress(asyncio.CancelledError):
                 loop.run_until_complete(task)
def main():
    parser = argparse.ArgumentParser(description='MadCars Bot Competition')
    parser.add_argument('-n',
                        '--num-games',
                        type=int,
                        help='Total number of games',
                        required=True)
    parser.add_argument('-d',
                        '--model-dir',
                        type=str,
                        help='NN model root directory',
                        required=True)
    parser.add_argument('-l',
                        '--log-interval',
                        type=int,
                        help='Rating print interval',
                        default=50)
    parser.add_argument('-c',
                        '--cache-path',
                        type=str,
                        default='competition_cache.json',
                        help='Ragings JSON cache')
    args = parser.parse_args()

    model_dir: str = args.model_dir
    num_games: int = args.num_games
    cache_path: str = args.cache_path
    log_interval: int = args.log_interval

    ts.setup(draw_probability=0.0001)
    loop = events.new_event_loop()
    events.set_event_loop(loop)

    clients = get_simple_bots() + get_nn_bots(model_dir)
    clients = load_ratings(cache_path, clients)

    games_played = 0

    while games_played < num_games:
        games_played += log_interval
        ratings = run_competition(clients, log_interval)
        ratings = sorted(ratings, key=lambda t: -ts.expose(t[1]))
        save_ratings(cache_path, clients)
        print(f'-- RATINGS {games_played} --')
        for name, rating in ratings:
            print(
                f'{ts.expose(rating):4.1f} ({rating.mu:4.1f} +- {rating.sigma * 3:4.1f}): {name:<32}'
            )
Example #33
0
    def native_run(main, *, debug=False):
        # Snatched from Python 3.7
        from asyncio import coroutines
        from asyncio import events
        from asyncio import tasks

        def _cancel_all_tasks(loop):
            to_cancel = all_tasks(loop)
            if not to_cancel:
                return

            for task in to_cancel:
                task.cancel()

            loop.run_until_complete(
                tasks.gather(*to_cancel, loop=loop, return_exceptions=True))

            for task in to_cancel:
                if task.cancelled():
                    continue
                if task.exception() is not None:
                    loop.call_exception_handler({
                        'message':
                        'unhandled exception during asyncio.run() shutdown',
                        'exception': task.exception(),
                        'task': task,
                    })

        if events._get_running_loop() is not None:
            raise RuntimeError(
                "asyncio.run() cannot be called from a running event loop")

        if not coroutines.iscoroutine(main):
            raise ValueError("a coroutine was expected, got {!r}".format(main))

        loop = events.new_event_loop()
        try:
            events.set_event_loop(loop)
            loop.set_debug(debug)
            return loop.run_until_complete(main)
        finally:
            try:
                _cancel_all_tasks(loop)
                loop.run_until_complete(loop.shutdown_asyncgens())
            finally:
                events.set_event_loop(None)
                loop.close()
Example #34
0
    def native_run(main: Awaitable[_T], *, debug: bool = False) -> _T:
        """Run a coroutine.

        This function runs the passed coroutine, taking care of
        managing the asyncio event loop and finalizing asynchronous
        generators.

        This function cannot be called when another asyncio event loop is
        running in the same thread.

        If debug is True, the event loop will be run in debug mode.

        This function always creates a new event loop and closes it at the end.
        It should be used as a main entry point for asyncio programs, and should
        ideally only be called once.

        Example:

            async def main():
                await asyncio.sleep(1)
                print('hello')

            asyncio.run(main())
        """
        from asyncio import events, coroutines

        if events._get_running_loop() is not None:
            raise RuntimeError(
                "asyncio.run() cannot be called from a running event loop")

        if not coroutines.iscoroutine(main):
            raise ValueError("a coroutine was expected, got {!r}".format(main))

        loop = events.new_event_loop()
        try:
            events.set_event_loop(loop)
            loop.set_debug(debug)
            return loop.run_until_complete(main)
        finally:
            try:
                _cancel_all_tasks(loop)
                loop.run_until_complete(loop.shutdown_asyncgens())
            finally:
                events.set_event_loop(None)  # type: ignore
                loop.close()
Example #35
0
    def async_run(main):
        """
        A simplified version of Python 3.7+ run
        """
        if events._get_running_loop() is not None:  # pylint: disable=protected-access
            raise RuntimeError(
                "asyncio.run() cannot be called from a running event loop"
            )

        if not coroutines.iscoroutine(main):
            raise ValueError("a coroutine was expected, got {!r}".format(main))

        loop = events.new_event_loop()
        try:
            events.set_event_loop(loop)
            return loop.run_until_complete(main)
        finally:
            events.set_event_loop(None)
            loop.close()
Example #36
0
 def setUp(self):
     super().setUp()
     self.loop = self.create_event_loop()
     events.set_event_loop(None)
Example #37
0
        print(len(self.done), 'completed tasks,', len(self.tasks),
              'still pending, todo', len(self.todo))


def main():
    loop = asyncio.get_event_loop()

    c = Crawler(sys.argv[1], loop)
    asyncio.Task(c.run())

    try:
        loop.add_signal_handler(signal.SIGINT, loop.stop)
    except RuntimeError:
        pass
    loop.run_forever()
    print('todo:', len(c.todo))
    print('busy:', len(c.busy))
    print('done:', len(c.done), '; ok:', sum(c.done.values()))
    print('tasks:', len(c.tasks))


if __name__ == '__main__':
    if '--iocp' in sys.argv:
        from asyncio import events, windows_events
        sys.argv.remove('--iocp')
        logging.info('using iocp')
        el = windows_events.ProactorEventLoop()
        events.set_event_loop(el)

    main()
Example #38
0
 def setUp(self):
     self.loop = base_events.BaseEventLoop()
     self.loop._selector = unittest.mock.Mock()
     events.set_event_loop(None)
Example #39
0
                    help='Max tick count',
                    default=1500)
parser.add_argument('-c',
                    '--console',
                    type=str,
                    nargs='?',
                    help='on/off run as console without drawing game objects.',
                    default='off')
parser.add_argument('--replay', help='Replay visio.gz')

args = parser.parse_args()

CONSTS.MAX_TICK_COUNT = args.max_tick_count

loop = events.new_event_loop()
events.set_event_loop(loop)

tcpClient = None


async def handle_connection(reader, writer):
    global tcpClient
    tcpClient = TcpClient(reader, writer, args.check_execution_limit == 'on')


async def client_wait_timeout():
    end_time = datetime.datetime.now() + datetime.timedelta(
        0, CONSTS.LR_CLIENT_WAIT_TIMEOUT)
    while not tcpClient and datetime.datetime.now() < end_time:
        await asyncio.sleep(0.1)
Example #40
0
#!/usr/bin/env python3

import aiohttp
import sys
import asyncio


def curl(url):
    response = yield from aiohttp.request('GET', url)
    print(repr(response))

    chunk = yield from response.content.read()
    print('Downloaded: %s' % len(chunk))

    response.close()


if __name__ == '__main__':
    if '--iocp' in sys.argv:
        from asyncio import events, windows_events
        sys.argv.remove('--iocp')
        el = windows_events.ProactorEventLoop()
        events.set_event_loop(el)

    loop = asyncio.get_event_loop()
    loop.run_until_complete(curl(sys.argv[1]))
Example #41
0
 def setUp(self):
     self.loop = unix_events.SelectorEventLoop()
     events.set_event_loop(None)
Example #42
0
 def setUp(self):
     self.loop = events.new_event_loop()
     events.set_event_loop(None)
Example #43
0
 def setUp(self):
     self.loop = events.new_event_loop()
     events.set_event_loop(None)
Example #44
0
 def set_event_loop(self, loop, *, cleanup=True):
     assert loop is not None
     # ensure that the event loop is passed explicitly in asyncio
     events.set_event_loop(None)
     if cleanup:
         self.addCleanup(self.close_loop, loop)
Example #45
0
import argparse
import asyncio
import aiohttp

def function1873(arg493):
    var1865 = aiohttp.ClientSession()
    var881 = yield from var1865.request('GET', arg493)
    print(repr(var881))
    var3637 = yield from var881.content.read()
    print(('Downloaded: %s' % len(var3637)))
    var881.close()
    yield from var1865.close()
if (__name__ == '__main__'):
    var627 = argparse.ArgumentParser(description='GET url example')
    var627.add_argument('url', nargs=1, metavar='URL', help='URL to download')
    var627.add_argument('--iocp', default=False, action='store_true', help='Use ProactorEventLoop on Windows')
    var3320 = var627.parse_args()
    if var3320.iocp:
        from asyncio import events, windows_events
        var3481 = windows_events.ProactorEventLoop()
        events.set_event_loop(var3481)
    var519 = asyncio.get_event_loop()
    var519.run_until_complete(function1873(var3320.url[0]))
Example #46
0
 def setUp(self):
     self.loop = base_events.BaseEventLoop()
     self.loop._selector = unittest.mock.Mock()
     events.set_event_loop(None)
Example #47
0
 def setUp(self):
     self.loop = test_utils.TestLoop()
     events.set_event_loop(None)
Example #48
0
 def setUp(self):
     super().setUp()
     events.set_event_loop(self.one_loop)
Example #49
0
 def set_event_loop(self, loop, *, cleanup=True):
     assert loop is not None
     # ensure that the event loop is passed explicitly in asyncio
     events.set_event_loop(None)
     if cleanup:
         self.addCleanup(self.close_loop, loop)
Example #50
0
 def tearDown(self):
     events.set_event_loop(None)
     super().tearDown()
Example #51
0
 def setUp(self):
     self.loop = test_utils.TestLoop()
     events.set_event_loop(None)