Skip to content

Commit ed46f75

Browse files
committed
Add func_utils and add new function/coroutine launcher in async_utils
1 parent 74c9c76 commit ed46f75

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

cloudbot/util/async_util.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
"""
44

55
import asyncio
6-
76
import sys
87
from functools import partial
98

9+
from cloudbot.util.func_utils import call_with_args
10+
1011

1112
def wrap_future(fut, *, loop=None):
1213
"""
@@ -33,6 +34,19 @@ def run_func(loop, func, *args, **kwargs):
3334
return (yield from loop.run_in_executor(None, part))
3435

3536

37+
@asyncio.coroutine
38+
def run_func_with_args(loop, func, arg_data, executor=None):
39+
if asyncio.iscoroutine(func):
40+
raise TypeError('A coroutine function or a normal, non-async callable are required')
41+
42+
if asyncio.iscoroutinefunction(func):
43+
coro = call_with_args(func, arg_data)
44+
else:
45+
coro = loop.run_in_executor(executor, call_with_args, func, arg_data)
46+
47+
return (yield from coro)
48+
49+
3650
def run_coroutine_threadsafe(coro, loop):
3751
"""
3852
Runs a coroutine in a threadsafe manner

cloudbot/util/func_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import inspect
2+
3+
4+
def call_with_args(func, arg_data):
5+
sig = inspect.signature(func)
6+
args = [arg_data[key] for key in sig.parameters.keys() if not key.startswith('_')]
7+
return func(*args)

0 commit comments

Comments
 (0)