Synchronous methods of Python tips become asynchronous methods

Get into the habit of writing together! This is the 8th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

Hello everyone, here is the long-awaited skill gentleman.

What I bring to you today is one of Python's super-powerful gameplay: 同步方法异步化.

background

In our usual FastApiwork, we often use some 异步的operations. In order to maintain consistency, we generally write supporting asynchronous code.

But if 我们提供了the functions of executable code like jmeter BeanShell are given to users, it 还能给你编写异步代码吗?is obviously impossible for users.

In another case, when we introduce third-party packages, such as some oss libraries, there are naturally synchronous methods and built-in requests. If you want to not block the entire fastapi service, you also need to make them asynchronous.

How to do it?

Give me some time to investigate this piece first, I haven't fully understood the content yet. The initial idea is to execute synchronous code through loop.run_in_executor, so that he can go beyond the three realms and not block it 事件循环.

Since it is not very sure, it will be clearly understood and added later. Is there no ready-made solution? The answer is yes, it's not just me who wants to do this!

For this purpose I have investigated several libraries and they all work well for this problem. Let's introduce them one by one.

Asyncer

First go to the address: asyncer.tiangolo.com/

This is the author of the famous FastApi, a tool written during leisure and entertainment. It can not only synchronize synchronous code, but also synchronize asynchronous code. Let's take a look at the introduction.

Simply look, the above synchronization method (sleep is used internally) has become an awaitable method under the package of asyncify, ie 异步化. However, since I didn't use it in real life, I would recommend it on the author's side.

AnyIO.run, since this library is based on AnyIO (another weird knowledge point), I think it should be similar to asyncio.run.

In short, since the author's introduction is in line with our expectations, we recommend it.

pip install asyncer
复制代码

awaitable

This library is very good, 乌拉written by an old brother. It is full of Russian comments that I can't understand, but I have tried it with relatively complete functions and easy to use. If you don't believe me, let's take a look:

  • Install
pip install awaits
复制代码
  • use

The difference from the above is that he just decorates the synchronous method and can use it as an asynchronous method, right 非常神奇?

I have tested, 疗效不错. Because it also has other features of asynchronous methods, such as cancellation. For details, please refer to the image sample code I introduced to the library below.

aioify

This is the first sync-to-async library I've come across, for which I also raised an issue for it. The picture shows whether I can do 取消a similar operation after testing a synchronous method asynchronously. This is a factor I use to tell if it's blocking.

At first, I found that if I encounter time.sleepsuch a synchronization method, this method does not seem to work, that is, it will still block. The author of this library later gave me a reply, to the effect that I did it again, and I can do it as expected. . I haven't tried it yet though, tried reading the source code, 无疾而终too.

pip install aioify
复制代码

Finally, I carefully read the source code of awaitable, which is relatively brief. If I have time, I will talk about it in detail. More importantly, I'm going to investigate the part that I said at the beginning. You can also be brave and try a few libraries described below.

I'm Milo, welcome to follow me, I will continue to "stock" in the Nuggets!

Guess you like

Origin juejin.im/post/7084213927311048740