Skip to main content

This python module enables asyncio based functions/methods to invoke classic synchroneous functions/methods. This is implemented by delegating execution of to a thread pool.

Project description

jk_asyncio_syncasync

Introduction

This python module enables asyncio based functions/methods to invoke classic synchroneous functions/methods. This is implemented by delegating execution of to a thread pool.

Information about this module can be found here:

Why this module?

Mixing synchroneous and asynchroneous code is not so easy and generally not recommended. Unfortunately sometimes you run into a situation where you can't avoid mixing both. For example this is the case if you need to invoke a synchroneous method implemented in a third party module where there is no asynchroneous counterpart implementation to the synchroneous one so that you can't avoid invoking the synchroneous code. In such a situation your call to a synchroneous function/method would block the asynchroneous task loop.

This module solves this problem by running the synchroneous method in a background thread without block the asynchroneous task loop. It therefore provides a safe way to invoke synchroneous code from within asynchroneous code.

Limitations of this module

This module focuses on asyncio (and asyncio) only. For trio this module is not required as trio already contains this functionality.

How to install module

This module can be installed easily using pip.

Use this command for a system wide installation of this module:

$ sudo pip install jk-asyncio-syncasync

Use this command for user specific installation of this module:

$ pip install --user jk-asyncio-syncasync

The PiPy module is always kept in sync with the Github repository so using PyPi is equivalent to a manual installation using the code provided on Github.

How to use this module

Import this module

Please include this module into your application using the following code:

from jk_asyncio_syncasync import *

Invoke a synchroneous function directly

Let's asume we have implemented a synchroneous function, for example this one:

def synchroneousFunction():
	print("Beginning synchroneous task ...")
	time.sleep(2)
	print("Synchroneous task completed.")

Let's now asume we want to invoke this function. This can be done with the following code using the function call_sync() provided by this module:

await call_sync(synchroneousFunction)

Invoke a synchroneous function using a decorator

Let's asume we have implemented a synchroneous function, for example this one:

def synchroneousFunction():
	print("Beginning synchroneous task ...")
	time.sleep(2)
	print("Synchroneous task completed.")

You can make use of a decorator in order to make this function asynchroneous:

@make_async
def synchroneousFunction():
	print("Beginning synchroneous task ...")
	time.sleep(2)
	print("Synchroneous task completed.")

Now invoking this function is easy:

await synchroneousFunction()

Invoking methods

It's perfectly safe to use call_sync() and the decorator @make_async with methods. Example:

Contact Information

This is Open Source code. That not only gives you the possibility of freely using this code it also allows you to contribute. Feel free to contact the author(s) of this software listed below, either for comments, collaboration requests, suggestions for improvement or reporting bugs:

License

This software is provided under the following license:

  • Apache Software License 2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jk_asyncio_syncasync-0.2019.10.19.tar.gz (7.2 kB view details)

Uploaded Source

File details

Details for the file jk_asyncio_syncasync-0.2019.10.19.tar.gz.

File metadata

  • Download URL: jk_asyncio_syncasync-0.2019.10.19.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.5.2

File hashes

Hashes for jk_asyncio_syncasync-0.2019.10.19.tar.gz
Algorithm Hash digest
SHA256 41f83ff523dd58f3f3490f9acf3b2dd26d641c579c80b0ecc1b38d8dcc1dc74b
MD5 1158e47bacc00a34f3ee0f0b4d9c1d43
BLAKE2b-256 c642cb9abf30c6e1207b4520c6f7f7e787a5be3d715bc18b0be6c13afff837c6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page