Пример #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)
Пример #2
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)
Пример #3
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)
Пример #4
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)
Пример #5
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)
Пример #6
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)
Пример #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)
Пример #8
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)
Пример #9
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)
Пример #10
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)
Пример #11
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)
Пример #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)
Пример #13
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)
Пример #14
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)
Пример #15
0
 def test_ctor_noloop(self):
     try:
         events.set_event_loop(self.loop)
         ev = locks.EventWaiter()
         self.assertIs(ev._loop, self.loop)
     finally:
         events.set_event_loop(None)
Пример #16
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)
Пример #17
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)
Пример #18
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)
Пример #19
0
 def test_open_connection_no_loop_ssl(self):
     with test_utils.run_test_server(self.loop, use_ssl=True) as httpd:
         try:
             events.set_event_loop(self.loop)
             f = streams.open_connection(*httpd.address, ssl=True)
             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'))
Пример #20
0
    def test_task_decorator_func(self):
        @tasks.task
        def notmuch():
            return 'ko'

        try:
            events.set_event_loop(self.loop)
            t = notmuch()
        finally:
            events.set_event_loop(None)

        self.assertIsInstance(t, tasks.Task)
        self.loop.run_until_complete(t)
        self.assertTrue(t.done())
        self.assertEqual(t.result(), 'ko')
Пример #21
0
 def setUp(self):
     super().setUp()
     self.loop = self.create_event_loop()
     events.set_event_loop(self.loop)
Пример #22
0
 def setUp(self):
     super().setUp()
     events.set_event_loop(self.one_loop)
Пример #23
0
 def setUp(self):
     self.loop = base_events.BaseEventLoop()
     self.loop._selector = unittest.mock.Mock()
     self.loop._selector.registered_count.return_value = 1
     events.set_event_loop(None)
Пример #24
0
        print(len(self.done), 'completed tasks,', len(self.tasks),
              'still pending, todo', len(self.todo))


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

    c = Crawler(sys.argv[1], loop)
    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 tulip import events, windows_events
        sys.argv.remove('--iocp')
        logging.info('using iocp')
        el = windows_events.ProactorEventLoop()
        events.set_event_loop(el)

    main()
Пример #25
0
 def tearDown(self):
     events.set_event_loop(None)
     super().tearDown()
Пример #26
0
 def setUp(self):
     self.loop = test_utils.TestLoop()
     events.set_event_loop(None)
Пример #27
0
 def setUp(self):
     self.loop = unix_events.SelectorEventLoop()
     events.set_event_loop(self.loop)
Пример #28
0
 def setUp(self):
     self.loop = unix_events.SelectorEventLoop()
     events.set_event_loop(None)
Пример #29
0
 def setUp(self):
     self.loop = test_utils.TestLoop()
     events.set_event_loop(None)
Пример #30
0
 def setUp(self):
     super().setUp()
     events.set_event_loop(self.one_loop)
Пример #31
0
 def tearDown(self):
     events.set_event_loop(None)
     super().tearDown()
Пример #32
0
 def setUp(self):
     self.loop = events.new_event_loop()
     events.set_event_loop(None)
Пример #33
0
 def setUp(self):
     self.loop = base_events.BaseEventLoop()
     self.loop._selector = unittest.mock.Mock()
     events.set_event_loop(None)
Пример #34
0
#!/usr/bin/env python3

import sys
import tulip
import tulip.http


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

    data = yield from response.read()
    print(data.decode('utf-8', 'replace'))


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

    loop = tulip.get_event_loop()
    loop.run_until_complete(curl(sys.argv[1]))
Пример #35
0
 def setUp(self):
     self.loop = base_events.BaseEventLoop()
     self.loop._selector = unittest.mock.Mock()
     events.set_event_loop(None)
Пример #36
0
 def setUp(self):
     self.loop = events.new_event_loop()
     events.set_event_loop(None)
Пример #37
0
 def setUp(self):
     self.loop = base_events.BaseEventLoop()
     self.loop._selector = unittest.mock.Mock()
     self.loop._selector.registered_count.return_value = 1
     events.set_event_loop(None)