Make writing a habit together! This is the 8th day of my participation in the “Gold Digging Day New Plan · April More Text Challenge”. Click here for more details.

Hello everyone, this is the long lost technique.

Today I’m going to introduce one of Python’s most powerful ways to do this: asynchronize synchronous methods.

background

In our FastApi work, we often use some asynchronous operations, to maintain consistency, we usually write asynchronous code.

But if we give users executable code like Jmeter BeanShell, can they still write asynchronous code for you? That is obviously an impossibility.

In another case, when we introduce third-party packages, such as some OSS libraries, which are naturally synchronous methods, with built-in requests requests, you don’t want to block the entire FastAPI service, but you also need to make them asynchronous.

How to do?

Please give me some time to research this part, I haven’t fully understood the content of it yet. The initial idea is to execute synchronized code through loop.run_in_executor, so that it goes beyond the three bounds and doesn’t block the event loop.

Because still not very certain, so will understand clearly in the back of the supplement come in. Isn’t there a plan in place? The answer is there, want to do this thing, not just me!

I have researched several libraries for this purpose, all of which do a good job of solving this problem. Let’s introduce them one by one.

Asyncer

Start with asyncer.tiangolo.com/

This is a tool that not only asynchronizes synchronous code, but also synchronizes asynchronous code. Let’s take a look at it.

For a quick look, the above synchronous method (which uses sleep internally) becomes the awaitable method wrapped in asyncify. But since I haven’t actually used it, for the sake of the author, I’d like to give you a recommendation.

AnyIO. Run, since this library is written based on AnyIO, I think it works just as well as Asyncio. run.

All in all, since the author’s introduction is in line with our expectations, we recommend it.

pip install asyncer
Copy the code

awaitable

It’s a nice library. It was written by an ullah. It’s full of Russian annotations that I don’t understand, but I’ve tried it and it’s easy to use. Let’s take a look:

  • The installation
pip install awaits
Copy the code
  • use

The difference with the above is that he just decorated the synchronous method, can be used as asynchronous method, is not very magical!

I’ve tested it. It works. Because it also has other features of asynchronous methods, such as cancellation. For details, see the image sample code I described for the library below.

aioify

This is the first sync-to-async library I’ve ever worked with, and I even mentioned issue for it. In the picture, I test whether a synchronous method can be asynchronized and cancel similar operations. This is the one factor I use to determine if it blocks.

At first, I found that if I encountered a synchronization method like time.sleep, this method did not work, that is, it would still block. But I haven’t tried it yet, tried to read the source code, nothing.

pip install aioify
Copy the code

Finally, I took a close look at the source code for Awaitable, which is quite brief, but I’ll talk about it in detail if I have time. More importantly, I’m going to investigate the opening paragraph. You can also be brave enough to try some of the libraries described below.

I’m Milo, welcome to follow me, I will continue to “buy” nuggets!