Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to change Python synchronous method to Asynchronous method

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article is a detailed introduction to "how Python synchronous method becomes asynchronous method". The content is detailed, the steps are clear, and the details are properly handled. I hope this article "how Python synchronous method becomes asynchronous method" can help you solve your doubts. Let's go deeper and learn new knowledge together with the ideas of the small editor.

background

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

But if we provide users with executable code like jmeter BeanShell, can users still write asynchronous code for you? That was obviously impossible.

There is also a situation when we introduce third-party packages, such as some oss libraries, which are naturally synchronous methods with built-in requests, and you want to not block the entire fastapi service, but also need to asynchronously them.

How?

Give me some time to investigate this first. I haven't fully understood the contents yet. The initial idea is to execute synchronization code through loop.run_in_executor, so that it is outside the three realms and does not block the event loop.

Since it is not very certain, it will be clearly understood later and added. Is there no plan? The answer is yes, I'm not the only one who wants to do this!

I researched several libraries for this purpose, and they all solved this problem very well. Let's introduce them one by one.

Asyncer

This is the famous FastApi author, leisure entertainment when writing a tool, it can not only synchronize code, asynchronous code can also be synchronized, we take a look at the introduction.

At a glance, the synchronous method above (sleep internally) becomes an awaitable method wrapped in asyncify, i.e. asynchronous. But since I haven't really used it, look at the author's face and give a recommendation.

AnyIO.run, since this library is written based on AnyIO (another strange knowledge point), I think it should be as effective as asyncio.run.

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

pip install asyncerawaitable

This library is quite good, it is written by a Ula brother. It's full of Russian comments I don't understand, but I tried it to be more functional and easy to use. Let's see if you don't believe me:

installation

pip install awaits

use

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

I tested it, and it worked. Because it also has other features of asynchronous methods, such as cancellation. Specific reference can be made to the following library I introduced the picture sample code.

aioify

This is the first synchronous to asynchronous library I've come across, and I raised an issue for it. Figure inside is I test a synchronous method asynchronous, can do cancel similar operations. This is one of the factors I use to determine whether it is blocked.

At first, I found that if I encountered a synchronization method such as time.sleep, this method did not seem to work, that is, it would still block. The author of the latter library replied to me, to the effect that I could do it again, and I could do it as expected. But I haven't tried it yet, tried reading the source code, and it ended without a disease.

pip install aioify Read here, this article "Python synchronous method how to become asynchronous method" article has been introduced, want to master the knowledge points of this article also need to practice to understand, if you want to know more about the content of the article, welcome to pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report