Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if function or method is normal or asynchronous

How can I find out if a function or method is a normal function or an async function? I would like my code to automatically support normal or async callbacks and need a way to test what type of function is passed.

async def exampleAsyncCb():     pass  def exampleNomralCb():     pass  def isAsync(someFunc):     #do cool dynamic python stuff on the function     return True/False  async def callCallback(cb, arg):     if isAsync(cb):         await cb(arg)     else:         cb(arg) 

And depending on what type of function gets passed it should either run it normally or with await. I tried various things but have no idea how to implement isAsync().

like image 948
Ecko Avatar asked Mar 18 '16 05:03

Ecko


People also ask

Is a function asynchronous?

An asynchronous function is any function that delivers its result asynchronously – for example, a callback-based function or a Promise-based function. An async function is defined via special syntax, involving the keywords async and await . It is also called async/await due to these two keywords.

How do you check if a function is Coroutine in Python?

Use the inspect module of Python. Return true if the object is a coroutine function (a function defined with an async def syntax). The inspect module provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects.


2 Answers

Use the inspect module of Python.

inspect.iscoroutinefunction(object)

Return true if the object is a coroutine function (a function defined with an async def syntax).

This function is available since Python 3.5. The module is available for Python 2 with lesser functionalities and certainly without the one you are looking for: inspect

Inspect module as the name suggests is useful to inspect a whole lot of thing. The documentation says

The inspect module provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects. For example, it can help you examine the contents of a class, retrieve the source code of a method, extract and format the argument list for a function, or get all the information you need to display a detailed traceback.

There are four main kinds of services provided by this module: type checking, getting source code, inspecting classes and functions, and examining the interpreter stack.

Some basic capabilities of this module are:

inspect.ismodule(object) inspect.isclass(object) inspect.ismethod(object) inspect.isfunction(object) 

It also packs capability to retrieve the source code

inspect.getdoc(object) inspect.getcomments(object) inspect.getfile(object)  inspect.getmodule(object) 

Methods are named intuitively. Description if needed can be found in documentation.

like image 108
Sharad Avatar answered Oct 12 '22 17:10

Sharad


If you don't want to introduce another import with inspect, iscoroutine is also available inside asyncio.

import asyncio  def isAsync(someFunc):     return asyncio.iscoroutinefunction(someFunc) 
like image 34
dirn Avatar answered Oct 12 '22 19:10

dirn