Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand the background asynchronism of python large projects

Shulou Source: shulou.com Published: 2022-06-03 07:27:59 09月10日 Update

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.

Tags: Task function background parameter information file utf-8 run content variable scenario multiple client time message end terminal process mail processing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker vpn NVidia OPPO Reno Linux