Skip to content

Commit 386e04a

Browse files
committed
Move asyncio methods into a separate module
1 parent 5942726 commit 386e04a

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

pydispatch/aioutils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import asyncio
2+
3+
class AioEmissionHoldLock(object):
4+
@property
5+
def aio_lock(self):
6+
l = getattr(self, '_aio_lock', None)
7+
if l is None:
8+
l = self._aio_lock = asyncio.Lock()
9+
return l
10+
async def __aenter__(self):
11+
await self.aio_lock.acquire()
12+
self.acquire()
13+
return self
14+
async def __aexit__(self, *args):
15+
self.aio_lock.release()
16+
self.release()

pydispatch/utils.py

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import sys
22
import weakref
33
import types
4-
import textwrap
5-
try:
6-
import asyncio
7-
except ImportError:
8-
asyncio = None
94

105
PY2 = sys.version_info.major == 2
116
if not PY2:
@@ -77,17 +72,11 @@ def __init__(self, **kwargs):
7772
def _data_del_callback(self, key):
7873
self.del_callback(key)
7974

80-
class EmissionHoldLock(object):
75+
class EmissionHoldLock_(object):
8176
def __init__(self, event_instance):
8277
self.event_instance = event_instance
8378
self.last_event = None
8479
self.held = False
85-
@property
86-
def aio_lock(self):
87-
l = getattr(self, '_aio_lock', None)
88-
if l is None:
89-
l = self._aio_lock = asyncio.Lock()
90-
return l
9180
def acquire(self):
9281
if self.held:
9382
return
@@ -101,18 +90,15 @@ def release(self):
10190
self.last_event = None
10291
self.held = False
10392
self.event_instance(*args, **kwargs)
104-
if AIO_AVAILABLE:
105-
exec(textwrap.dedent('''
106-
async def __aenter__(self):
107-
await self.aio_lock.acquire()
108-
self.acquire()
109-
return self
110-
async def __aexit__(self, *args):
111-
self.aio_lock.release()
112-
self.release()
113-
'''))
11493
def __enter__(self):
11594
self.acquire()
11695
return self
11796
def __exit__(self, *args):
11897
self.release()
98+
99+
if AIO_AVAILABLE:
100+
from pydispatch.aioutils import AioEmissionHoldLock
101+
class EmissionHoldLock(EmissionHoldLock_, AioEmissionHoldLock):
102+
pass
103+
else:
104+
EmissionHoldLock = EmissionHoldLock_

0 commit comments

Comments
 (0)