Skip to main content

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

Metadata

Release files for jk-asyncio-syncasync 0.2019.10.19

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jk-asyncio-syncasync 0.2019.10.19
File Size Uploaded
jk_asyncio_syncasync-0.2019.10.19.tar.gz 7.2 kB Details

Release files / jk_asyncio_syncasync-0.2019.10.19.tar.gz

Download URL jk_asyncio_syncasync-0.2019.10.19.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
41f83ff523dd58f3f3490f9acf3b2dd26d641c579c80b0ecc1b38d8dcc1dc74b
BLAKE2b-256 checksum
How to use checksums
c642cb9abf30c6e1207b4520c6f7f7e787a5be3d715bc18b0be6c13afff837c6
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.2019.10.19 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page