Skip to content

Instantly share code, notes, and snippets.

@Leechael
Created November 30, 2016 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Leechael/5b0aeacdd3f4f25666f637b18a99b9a7 to your computer and use it in GitHub Desktop.
Save Leechael/5b0aeacdd3f4f25666f637b18a99b9a7 to your computer and use it in GitHub Desktop.
Buggy: inspect.iscoroutinefunction and inspect.isfunction :(
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import inspect
def iscoroutinefunction(fn):
if inspect.iscoroutinefunction(fn):
return True
if inspect.iscoroutinefunction(getattr(fn, "__call__", None)):
return True
return False
async def execute(fn, *args, **kwargs):
if callable(fn):
if iscoroutinefunction(fn):
result = await fn(*args, **kwargs)
else:
result = fn(*args, **kwargs)
else:
raise RuntimeError(
"Expected argument `fn` is callable."
)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment