Package: jk-asyncio-syncasync
jk_asyncio_syncasyncIntroductionThis 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:github.orgpypi.python.orgWhy 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 moduleThis module focuses onasyncio(andasyncio) only. Fortriothis module is not required astrioalready contains this functionality.How to install moduleThis module can be installed easily usingpip.Use this command for a system wide installation of this module:$sudopipinstalljk-asyncio-syncasyncUse this command for user specific installation of this module:$pipinstall--userjk-asyncio-syncasyncThe 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 moduleImport this modulePlease include this module into your application using the following code:fromjk_asyncio_syncasyncimport*Invoke a synchroneous function directlyLet's asume we have implemented a synchroneous function, for example this one:defsynchroneousFunction():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 functioncall_sync()provided by this module:awaitcall_sync(synchroneousFunction)Invoke a synchroneous function using a decoratorLet's asume we have implemented a synchroneous function, for example this one:defsynchroneousFunction():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_asyncdefsynchroneousFunction():print("Beginning synchroneous task ...")time.sleep(2)print("Synchroneous task completed.")Now invoking this function is easy:awaitsynchroneousFunction()Invoking methodsIt's perfectly safe to usecall_sync()and the decorator@make_asyncwith methods. Example:Contact InformationThis 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:Jürgen Knauth:jknauth@uni-goettingen.de,pubsrc@binary-overflow.deLicenseThis software is provided under the following license:Apache Software License 2.0