Skip to content

Commit 193ee32

Browse files
committed
Add EventLoopPolicy
1 parent af1687b commit 193ee32

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

tests/test_base.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import time
55

66
import uvloop
7+
import unittest
78

89
from unittest import mock
910
from uvloop._testbase import UVTestCase, AIOTestCase
@@ -398,3 +399,17 @@ class TestBaseUV(_TestBase, UVTestCase):
398399

399400
class TestBaseAIO(_TestBase, AIOTestCase):
400401
pass
402+
403+
404+
class TestPolicy(unittest.TestCase):
405+
406+
def test_uvloop_policy(self):
407+
try:
408+
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
409+
loop = asyncio.new_event_loop()
410+
try:
411+
self.assertIsInstance(loop, uvloop._Loop)
412+
finally:
413+
loop.close()
414+
finally:
415+
asyncio.set_event_loop_policy(None)

uvloop/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
11
import asyncio
22

3+
from asyncio.events import BaseDefaultEventLoopPolicy as __BasePolicy
4+
35
from . import includes as __includes
46
from .loop import Loop as __BaseLoop
57

68

9+
__all__ = ('new_event_loop', 'EventLoopPolicy')
10+
11+
712
class _Loop(__BaseLoop, asyncio.AbstractEventLoop):
813
pass
914

1015

1116
def new_event_loop():
17+
"""Return a new event loop."""
1218
return _Loop()
19+
20+
21+
class EventLoopPolicy(__BasePolicy):
22+
"""Event loop policy.
23+
24+
The preferred way to make your application use uvloop:
25+
26+
>>> import asyncio
27+
>>> import uvloop
28+
>>> asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
29+
>>> asyncio.get_event_loop()
30+
<uvloop._Loop running=False closed=False debug=False>
31+
"""
32+
33+
def _loop_factory(self):
34+
return new_event_loop()

uvloop/loop.pyx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -764,9 +764,12 @@ cdef class Loop:
764764
# Public API
765765

766766
def __repr__(self):
767-
return ('<%s running=%s closed=%s debug=%s>'
768-
% (self.__class__.__name__, self.is_running(),
769-
self.is_closed(), self.get_debug()))
767+
return '<{}.{} running={} closed={} debug={}>'.format(
768+
self.__class__.__module__,
769+
self.__class__.__name__,
770+
self.is_running(),
771+
self.is_closed(),
772+
self.get_debug())
770773

771774
def call_soon(self, callback, *args):
772775
if self._debug == 1:

0 commit comments

Comments
 (0)