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 understand the background asynchronism of python large projects

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

How to understand python large-scale project background asynchronous, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

one

BackgroundTasks usage scenario

Sometimes we need to continue some operations after the request is executed, but the terminal does not have to wait for these operations to complete before receiving the response. Let me list some scenes for you to take a look at:

1. After the automatic ticket issue is completed, the itinerary information needs to be automatically sent to each ota platform.

two。 After the purchase of the ticket, you need to send an email to each household to notify the success of the purchase.

3. After receiving the file from the client, the file is processed again.

4....

5....

These operations require a certain amount of processing time, but are not directly related to the response returned to the terminal. At this time, you can define the background task BackgroundTasks to achieve this function.

two

BackgroundTasks actual combat

2.1

Add parameter

First we need to import BackgroundTasks and add parameters of type BackgroundTasks to the path operation function.

#-*-encoding: utf-8-*-from fastapi import BackgroundTasks, FastAPIapp = FastAPI () @ app.post ("/ send_info") async def send_notification (email: str, background_tasks: BackgroundTasks): "" send reminder task "pass

2.2

Task function

A task function is a function that needs to be created that is actually executed in a background task. The task function is a standard function. This function can be a function of async def or a normal def.

Here you create a function that writes the specified content to the file.

#-*-encoding: utf-8-*-def write_notification (email: str, message= ""): "write file operation simulation task"with open (" log_test.txt ", mode=" w ") as email_file: content =" notification for {}: {} ".format (email,message) email_file.write (content)

2.3

Add background task

Finally, you need to add the task function to the background task.

#-*-encoding: utf-8-*-import timefrom fastapi import BackgroundTasks, FastAPIapp = FastAPI () def write_notification (email: str, message= ""): "write file operation simulation task"time.sleep (5) with open (" log_test.txt ", mode=" w ") as email_file: content =" notification for {}: {} ".format (email Message) email_file.write (content) print ("write end") time.sleep (2) @ app.post ("/ send_info") async def send_notification (email: str, background_tasks: BackgroundTasks): background_tasks.add_task (write_notification, email, message= "some notification") return {"message": "now:% s Notification sent in the background" >

2.4

Add_task interpretation

.add _ task () receives the following parameter information: 1. Task functions that run in the background (for example, write_notification) 2. Parameter information of any sequence (for example, email) 3. Parameter information of any key-value pair (for example, message= "some notification") 4. We deliberately added a wait time to the write_notification method to verify the return to the client

2.5

Dependency injection

Background tasks can be used with dependency injection systems to declare BackgroundTasks parameters in different levels of dependencies

#-*-encoding: utf-8-*-from typing import Optionalfrom fastapi import BackgroundTasks, Depends, FastAPIapp = FastAPI () def write_log (message: str): with open ("log.txt", mode= "a") as log: log.write (message) def get_query (background_tasks: BackgroundTasks, Q: Optional [str] = None): if Q: message = f "found query: {Q}\ n" background_tasks.add_task (write_log Message) return q@app.post ("/ send_info") async def send_notification (email: str, background_tasks: BackgroundTasks, Q: str = Depends (get_query)): message = f "message to {email}\ n" background_tasks.add_task (write_log, message) return {"message": "Message sent"}

1. If you need to perform a large number of background calculations without having to run in the same process, for example, it does not require shared memory, variables, etc., you can use other larger tools, such as celery and MQ series, but these often require more complex configurations, such as message job queue managers such as RabbitMQ and Redis. But they allow background tasks to be run in multiple processes, especially multiple servers.

two。 If you need to access variables and objects from the same FastAPI application, or need to perform some small background tasks such as sending email, SMS messages, etc., you can simply use BackgroundTasks.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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