Skip to content

Commit 5d6bb5b

Browse files
committed
Add wrapper function for asyncio.async()/asyncio.ensure_future()
1 parent 899cd32 commit 5d6bb5b

6 files changed

Lines changed: 44 additions & 12 deletions

File tree

cloudbot/__main__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
import signal
77

88
# store the original working directory, for use when restarting
9+
from functools import partial
10+
11+
from cloudbot.util import async_util
12+
913
original_wd = os.path.realpath(".")
1014

1115
# set up environment - we need to make sure we are in the install directory
@@ -47,7 +51,8 @@ def exit_gracefully(signum, frame):
4751
stopped_while_restarting = True
4852
else:
4953
_bot.loop.call_soon_threadsafe(
50-
lambda: asyncio.async(_bot.stop("Killed (Received SIGINT {})".format(signum)), loop=_bot.loop))
54+
partial(async_util.wrap_future, _bot.stop("Killed (Received SIGINT {})".format(signum)), loop=_bot.loop)
55+
)
5156

5257
logger.warning("Bot received Signal Interrupt ({})".format(signum))
5358

cloudbot/clients/irc.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from cloudbot.client import Client
99
from cloudbot.event import Event, EventType
10+
from cloudbot.util import async_util
1011

1112
logger = logging.getLogger("cloudbot")
1213

@@ -218,8 +219,7 @@ def _send(self, line):
218219
:type line: str
219220
"""
220221
logger.info("[{}] >> {}".format(self.name, line))
221-
asyncio.async(self._protocol.send(line), loop=self.loop)
222-
222+
async_util.wrap_future(self._protocol.send(line), loop=self.loop)
223223

224224
@property
225225
def connected(self):
@@ -272,14 +272,14 @@ def connection_lost(self, exc):
272272
# we've been closed intentionally, so don't reconnect
273273
return
274274
logger.error("[{}] Connection lost: {}".format(self.conn.name, exc))
275-
asyncio.async(self.conn.connect(), loop=self.loop)
275+
async_util.wrap_future(self.conn.connect(), loop=self.loop)
276276

277277
def eof_received(self):
278278
self._connected = False
279279
# create a new connected_future for when we are connected.
280280
self._connected_future = asyncio.Future(loop=self.loop)
281281
logger.info("[{}] EOF received.".format(self.conn.name))
282-
asyncio.async(self.conn.connect(), loop=self.loop)
282+
async_util.wrap_future(self.conn.connect(), loop=self.loop)
283283
return True
284284

285285
@asyncio.coroutine
@@ -339,7 +339,7 @@ def data_received(self, data):
339339
# Reply to pings immediately
340340

341341
if command == "PING":
342-
asyncio.async(self.send("PONG " + command_params[-1]), loop=self.loop)
342+
async_util.wrap_future(self.send("PONG " + command_params[-1]), loop=self.loop)
343343

344344
# Parse the command and params
345345

@@ -406,7 +406,7 @@ def data_received(self, data):
406406
irc_prefix=prefix, irc_command=command, irc_paramlist=command_params, irc_ctcp_text=ctcp_text)
407407

408408
# handle the message, async
409-
asyncio.async(self.bot.process(event), loop=self.loop)
409+
async_util.wrap_future(self.bot.process(event), loop=self.loop)
410410

411411
# Channel Commands
412412
# NOTICE #chan :Text

cloudbot/plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from cloudbot.event import Event
1919
from cloudbot.hook import Priority, Action
20-
from cloudbot.util import database
20+
from cloudbot.util import database, async_util
2121

2222
logger = logging.getLogger("cloudbot")
2323

@@ -204,7 +204,7 @@ def load_plugin(self, path):
204204
self._log_hook(on_cap_ack_hook)
205205

206206
for periodic_hook in plugin.periodic:
207-
task = asyncio.async(self._start_periodic(periodic_hook))
207+
task = async_util.wrap_future(self._start_periodic(periodic_hook))
208208
plugin.tasks.append(task)
209209
self._log_hook(periodic_hook)
210210

cloudbot/reloader.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import asyncio
22
import os.path
3+
from functools import partial
34

45
from watchdog.observers import Observer
56
from watchdog.events import PatternMatchingEventHandler
67

8+
from cloudbot.util import async_util
9+
710

811
class PluginReloader(object):
912
def __init__(self, bot):
@@ -38,7 +41,8 @@ def reload(self, path):
3841

3942
if isinstance(path, bytes):
4043
path = path.decode()
41-
self.bot.loop.call_soon_threadsafe(lambda: asyncio.async(self._reload(path), loop=self.bot.loop))
44+
45+
self.bot.loop.call_soon_threadsafe(partial(async_util.wrap_future, self._reload(path), loop=self.bot.loop))
4246

4347
def unload(self, path):
4448
"""
@@ -48,7 +52,8 @@ def unload(self, path):
4852
"""
4953
if isinstance(path, bytes):
5054
path = path.decode()
51-
self.bot.loop.call_soon_threadsafe(lambda: asyncio.async(self._unload(path), loop=self.bot.loop))
55+
56+
self.bot.loop.call_soon_threadsafe(partial(async_util.wrap_future, self._unload(path), loop=self.bot.loop))
5257

5358
@asyncio.coroutine
5459
def _reload(self, path):

cloudbot/util/async_util.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
Wraps various asyncio functions
3+
"""
4+
5+
import asyncio
6+
7+
import sys
8+
9+
10+
def wrap_future(fut, *, loop=None):
11+
"""
12+
Wraps asyncio.async()/asyncio.ensure_future() depending on the python version
13+
:param fut: The awaitable, future, or coroutine to wrap
14+
:param loop: The loop to run in
15+
:return: The wrapped future
16+
"""
17+
if sys.version_info < (3, 4, 4):
18+
return asyncio.async(fut, loop=loop)
19+
20+
return asyncio.ensure_future(fut, loop=loop)

plugins/geoip.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import geoip2.errors
1111

1212
from cloudbot import hook
13+
from cloudbot.util import async_util
1314

1415
logger = logging.getLogger("cloudbot")
1516

@@ -52,6 +53,7 @@ def update_db():
5253
return geoip2.database.Reader(PATH)
5354

5455

56+
@asyncio.coroutine
5557
def check_db(loop):
5658
"""
5759
runs update_db in an executor thread and sets geoip_reader to the result
@@ -68,7 +70,7 @@ def check_db(loop):
6870
@asyncio.coroutine
6971
@hook.on_start
7072
def load_geoip(loop):
71-
asyncio.async(check_db(loop), loop=loop)
73+
async_util.wrap_future(check_db(loop), loop=loop)
7274

7375

7476
@asyncio.coroutine

0 commit comments

Comments
 (0)