Add EventLoopPolicy

This commit is contained in:
Yury Selivanov 2016-04-19 15:28:26 -04:00
parent af1687b5de
commit 193ee328d3
3 changed files with 43 additions and 3 deletions

View File

@ -4,6 +4,7 @@ import threading
import time
import uvloop
import unittest
from unittest import mock
from uvloop._testbase import UVTestCase, AIOTestCase
@ -398,3 +399,17 @@ class TestBaseUV(_TestBase, UVTestCase):
class TestBaseAIO(_TestBase, AIOTestCase):
pass
class TestPolicy(unittest.TestCase):
def test_uvloop_policy(self):
try:
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
loop = asyncio.new_event_loop()
try:
self.assertIsInstance(loop, uvloop._Loop)
finally:
loop.close()
finally:
asyncio.set_event_loop_policy(None)

View File

@ -1,12 +1,34 @@
import asyncio
from asyncio.events import BaseDefaultEventLoopPolicy as __BasePolicy
from . import includes as __includes
from .loop import Loop as __BaseLoop
__all__ = ('new_event_loop', 'EventLoopPolicy')
class _Loop(__BaseLoop, asyncio.AbstractEventLoop):
pass
def new_event_loop():
"""Return a new event loop."""
return _Loop()
class EventLoopPolicy(__BasePolicy):
"""Event loop policy.
The preferred way to make your application use uvloop:
>>> import asyncio
>>> import uvloop
>>> asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
>>> asyncio.get_event_loop()
<uvloop._Loop running=False closed=False debug=False>
"""
def _loop_factory(self):
return new_event_loop()

View File

@ -764,9 +764,12 @@ cdef class Loop:
# Public API
def __repr__(self):
return ('<%s running=%s closed=%s debug=%s>'
% (self.__class__.__name__, self.is_running(),
self.is_closed(), self.get_debug()))
return '<{}.{} running={} closed={} debug={}>'.format(
self.__class__.__module__,
self.__class__.__name__,
self.is_running(),
self.is_closed(),
self.get_debug())
def call_soon(self, callback, *args):
if self._debug == 1: