|
2 | 2 | import concurrent.futures |
3 | 3 | import enum |
4 | 4 | import logging |
| 5 | +import warnings |
| 6 | +from functools import partial |
| 7 | + |
| 8 | +import sys |
5 | 9 |
|
6 | 10 | logger = logging.getLogger("cloudbot") |
7 | 11 |
|
@@ -153,7 +157,7 @@ def prepare(self): |
153 | 157 | # we're running a coroutine hook with a db, so initialise an executor pool |
154 | 158 | self.db_executor = concurrent.futures.ThreadPoolExecutor(1) |
155 | 159 | # be sure to initialize the db in the database executor, so it will be accessible in that thread. |
156 | | - self.db = yield from self.async(self.bot.db_session) |
| 160 | + self.db = yield from self.async_call(self.bot.db_session) |
157 | 161 |
|
158 | 162 | def prepare_threaded(self): |
159 | 163 | """ |
@@ -189,7 +193,7 @@ def close(self): |
189 | 193 | if self.db is not None: |
190 | 194 | #logger.debug("Closing database session for {}:threaded=False".format(self.hook.description)) |
191 | 195 | # be sure the close the database in the database executor, as it is only accessable in that one thread |
192 | | - yield from self.async(self.db.close) |
| 196 | + yield from self.async_call(self.db.close) |
193 | 197 | self.db = None |
194 | 198 |
|
195 | 199 | def close_threaded(self): |
@@ -310,17 +314,34 @@ def has_permission(self, permission, notice=True): |
310 | 314 | return self.conn.permissions.has_perm_mask(self.mask, permission, notice=notice) |
311 | 315 |
|
312 | 316 | @asyncio.coroutine |
313 | | - def async(self, function, *args, **kwargs): |
| 317 | + def async_call(self, func, *args, **kwargs): |
314 | 318 | if self.db_executor is not None: |
315 | 319 | executor = self.db_executor |
316 | 320 | else: |
317 | 321 | executor = None |
318 | | - if kwargs: |
319 | | - result = yield from self.loop.run_in_executor(executor, function, *args) |
320 | | - else: |
321 | | - result = yield from self.loop.run_in_executor(executor, lambda: function(*args, **kwargs)) |
| 322 | + |
| 323 | + part = partial(func, *args, **kwargs) |
| 324 | + result = yield from self.loop.run_in_executor(executor, part) |
322 | 325 | return result |
323 | 326 |
|
| 327 | + if sys.version_info < (3, 7, 0): |
| 328 | + # noinspection PyCompatibility |
| 329 | + @asyncio.coroutine |
| 330 | + def async_(self, function, *args, **kwargs): |
| 331 | + warnings.warn( |
| 332 | + "event.async() is deprecated, use event.async_call() instead.", |
| 333 | + DeprecationWarning, stacklevel=2 |
| 334 | + ) |
| 335 | + result = yield from self.async_call(function, *args, **kwargs) |
| 336 | + return result |
| 337 | + |
| 338 | + |
| 339 | +# Silence deprecation warnings about use of the 'async' name as a function |
| 340 | +try: |
| 341 | + setattr(Event, 'async', getattr(Event, 'async_')) |
| 342 | +except AttributeError: |
| 343 | + pass |
| 344 | + |
324 | 345 |
|
325 | 346 | class CommandEvent(Event): |
326 | 347 | """ |
|
0 commit comments