Hướng dẫn python run async function in background - python chạy hàm không đồng bộ trong nền

Tôi có một số chức năng không đồng bộ. Nếu tôi gọi _main(), tôi không thể sử dụng hàm _check() thứ hai, vì _main() chạy liên tục trong vòng lặp một thời gian. Tôi có thể chạy _main() 'trong nền' để cung cấp phần còn lại của các chức năng không?

import asyncio


async def _main(self):
    while True:
        ...
        Update database
        ...


async def _check(self):
    await self._confirm_update()


async def process(self):
    if self.command in ["main"]:
        await self._main()
    elif self.command in ["check"]:
        await self._check()

hỏi ngày 26 tháng 1 năm 2021 lúc 16:13Jan 26, 2021 at 16:13

Hướng dẫn python run async function in background - python chạy hàm không đồng bộ trong nền

2

Tôi có thể chạy _main () 'trong nền' để cung cấp phần còn lại của các hàm không?

Có, bạn có thể thay thế

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
1 bằng
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
2. Điều đó sẽ sinh ra
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
3 như một nhiệm vụ chạy hiệu quả trong nền.

Đã trả lời ngày 27 tháng 1 năm 2021 lúc 15:02Jan 27, 2021 at 15:02

user4815162342user4815162342user4815162342

Huy hiệu vàng 131K1515 gold badges258 silver badges323 bronze badges

Những gì bạn thực sự yêu cầu, là làm thế nào để đạt được sự đồng thời (một số coroutines/"những thứ" chạy đồng thời). Bạn có thể đạt được sự đồng thời bằng cách sử dụng các nhiệm vụ nền như câu trả lời đầu tiên cho thấy.

Tuy nhiên, chiến lược đó kết thúc dễ dàng trong một mớ hỗn độn (tốt, đồng thời luôn kết thúc trong mớ hỗn độn): bạn có thể nhận được các nhiệm vụ nền "Runaway" mà bạn không biết vẫn đang chạy, các tác vụ nền tảng điều khiển cùng một cấu trúc dữ liệu, dẫn đến bất ngờ , logic của chương trình của bạn cũng có thể phụ thuộc nếu các tác vụ kết thúc hay không (để kích hoạt các nhiệm vụ khác nhau) và sau đó bạn bắt đầu gây rối với asyncio.wait và asyncio.gather. Đó là một con đường đến địa ngục, thực sự.

Vì vậy, tôi đang quảng cáo ở đây một cách xấu hổ một điều tôi đã viết để giải quyết các vấn đề này: TaskThread. Đó là một cuộc sống thực an toàn hơn: bạn có thể điều hành hàng tấn các nhiệm vụ nền đồng thời, nhưng chúng được kiểm soát và được tổ chức tốt.

Đã trả lời ngày 15 tháng 3 lúc 13:27Mar 15 at 13:27

Hướng dẫn python run async function in background - python chạy hàm không đồng bộ trong nền

El Sampel SampsaEl Sampsa

1.5833 huy hiệu vàng16 Huy hiệu bạc33 Huy hiệu đồng3 gold badges16 silver badges33 bronze badges

chờ đợi asyncio. Ngủ (1).

In ('Thế giới!').

# Python 3.7+.

Làm thế nào để bạn chờ đợi chức năng async trong python?

Hàm Async sử dụng từ khóa đang chờ đợi để biểu thị một coroutine. Khi sử dụng từ khóa đang chờ, Coroutines giải phóng luồng điều khiển trở lại vòng lặp sự kiện. Để chạy một coroutine, chúng ta cần lên lịch cho vòng lặp sự kiện. Sau khi lên lịch, các coroutines được gói trong các nhiệm vụ như một đối tượng trong tương lai.stop at line 8 and scheduled it to call after 300 seconds. So the job monitor is going to die after 5 minutes.

Trong tuần trước, tôi đã triển khai một máy chủ công việc trong Python 3.7. Trong dự án này, chúng tôi cần chạy một thành phần để kiểm tra trạng thái của các công việc được kích hoạt trên các quy trình của công nhân. Thay vì chạy một quy trình khác trên cụm, tôi thích chạy nó như một coroutine cùng với máy chủ HTTP.

Đây là một khá đơn giản để làm với Asyncio. Không cần phải sử dụng gói luồng Python.

# Python 3.7+.asyncio. Async programming allows you to write concurrent code that runs in a single thread.

Làm thế nào để bạn chờ đợi chức năng async trong python? Asyncio doesn’t use threads or multiprocessing to make the program Asynchronous.

Hàm Async sử dụng từ khóa đang chờ đợi để biểu thị một coroutine. Khi sử dụng từ khóa đang chờ, Coroutines giải phóng luồng điều khiển trở lại vòng lặp sự kiện. Để chạy một coroutine, chúng ta cần lên lịch cho vòng lặp sự kiện. Sau khi lên lịch, các coroutines được gói trong các nhiệm vụ như một đối tượng trong tương lai.: Coroutines are a general control structure whereby flow control is cooperatively passed between two different routines without returning. In asyncio Coroutine can be created by using async keyword before def.                    

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")

Lập trình không đồng bộ là một loại lập trình trong đó chúng ta có thể thực hiện nhiều hơn một nhiệm vụ mà không chặn nhiệm vụ chính (chức năng). Trong Python, có nhiều cách để thực hiện nhiều hơn một chức năng đồng thời, một trong những cách là bằng cách sử dụng asyncio. Lập trình ASYNC cho phép bạn viết mã đồng thời chạy trong một luồng duy nhất.

Lưu ý: Asyncio không sử dụng các chủ đề hoặc đa xử lý để làm cho chương trình không đồng bộ.

speak_async()

Coroutine: Coroutines là một cấu trúc kiểm soát chung, theo đó kiểm soát dòng chảy được thông qua hợp tác giữa hai thói quen khác nhau mà không quay trở lại. Trong Asyncio Coroutine có thể được tạo bằng cách sử dụng từ khóa Async trước Def. & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;

Nếu bạn cố gắng chạy chức năng Async trực tiếp, bạn sẽ nhận được cảnh báo thời gian chạy:You can think of Event Loop as functions to run asynchronous tasks and callbacks, perform network IO operations, and run subprocesses. 

RunTimewarning: Coroutine ‘speak_async, không bao giờ được chờ đợi Event Loop example to run async Function to run a single async function:

Python3

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
4
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
5

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
6
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
7
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
8

Để chạy chức năng Async (Coroutine), bạn phải gọi nó bằng cách sử dụng vòng lặp sự kiện.

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
7
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
8
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
4
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
0
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
1

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
7
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
8
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
4
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
5
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
1

Các vòng lặp sự kiện: Bạn có thể nghĩ về vòng lặp sự kiện là các chức năng để chạy các tác vụ và cuộc gọi lại không đồng bộ, thực hiện các hoạt động IO mạng và chạy các quy trình con. & NBSP;

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
7
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
1
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
01
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
1
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
03

Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
3

Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
4

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
8
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
4
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
7
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
1

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
8
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
4
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
7
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
1

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
7
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
.
.
.
7
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
1
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
.
.
.
9 _main()0
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
1
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
1

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
6
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
7 _main()0
Execute more than one function at a time. To do so we have to create a new async function (main) and call all the async functions (which we want to run at the same time) in that new function (main). And then call the new (main) function using Event Loops…

Code:

Python3

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
4
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
5

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
6
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
7
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
8

Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
0
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
1
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
2

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
7
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
.
.
.
7
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
1
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
.
.
.
9 _main()0
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
1
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
1

_main()5

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
8
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
4
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
0
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
1

_main()5

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
8
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
4
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
5
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
1

Các vòng lặp sự kiện: Bạn có thể nghĩ về vòng lặp sự kiện là các chức năng để chạy các tác vụ và cuộc gọi lại không đồng bộ, thực hiện các hoạt động IO mạng và chạy các quy trình con. & NBSP;

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
6
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
7 _main()0

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
9
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
8
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
4_main()4
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
1

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
9
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
8
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
9

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
6
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
7
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
01

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
9
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
03
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
1
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
05

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
9
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
07
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
1
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
09

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
9
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
11

Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
0
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
1
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
2

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
15

Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
4

Đầu ra

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Lưu ý: .create_task () được sử dụng để chạy nhiều hàm async tại một thời điểm..create_task() is used to run multiple async functions at a time.

Ví dụ 3: Ở đây bạn có thể thấy function_async () và function_2 () không chạy đồng thời, đầu ra của function_async () được hiển thị trước và sau đó đầu ra của function_2 () được hiển thị, có nghĩa là function_2 () đang được thực thi sau khi thực thi của function_async ().Here you can see function_async() and function_2() are not running concurrently, the output of function_async() is displayed first and then the output of function_2() is displayed, that means function_2() is being executed after the execution of function_async().

Nhưng chúng tôi không muốn điều đó! Chúng tôi muốn cả hai chức năng để đạt được tiến bộ đồng thời, vì vậy để đạt được điều đó trong Python, chúng tôi phải nói rõ ràng cho máy tính chuyển từ chức năng này sang chức năng khác.

Code:

Python3

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
4
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
5

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
6
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
7
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
8

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
9
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
0
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
1
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
2
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
3
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
4
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
.
.
.
44

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
7
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
.
.
.
7
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
1
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
.
.
.
9 _main()0
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
1
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
1

_main()5

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
8
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
4
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
0
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
1

_main()5

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
8
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
4
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
5
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
1

_main()5

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
50
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
51
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
1

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
9
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
8
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
9

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
6
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
7 _main()0

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
9
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
8
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
4_main()4
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
1

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
9
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
8
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
9

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
6
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
7
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
01

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
9
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
03
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
1
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
05

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
9
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
07
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
1
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
09

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
9
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
11

Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
0
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
1
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
2

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
15

Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
4

Đầu ra

Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great

Lưu ý: .create_task () được sử dụng để chạy nhiều hàm async tại một thời điểm.

Ví dụ 3: Ở đây bạn có thể thấy function_async () và function_2 () không chạy đồng thời, đầu ra của function_async () được hiển thị trước và sau đó đầu ra của function_2 () được hiển thị, có nghĩa là function_2 () đang được thực thi sau khi thực thi của function_async ().

Nhưng chúng tôi không muốn điều đó! Chúng tôi muốn cả hai chức năng để đạt được tiến bộ đồng thời, vì vậy để đạt được điều đó trong Python, chúng tôi phải nói rõ ràng cho máy tính chuyển từ chức năng này sang chức năng khác.Just use the while True loop in the main function:

Python3

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
4
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
5

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
6
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
7
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
8

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
9
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
0
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
1
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
2
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
3
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
4
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
.
.
.
44

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
7
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
.
.
.
7
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
1
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
.
.
.
9 _main()0
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
1
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
1

_main()5

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
8
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
4
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
5
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
1

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
7
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
.
.
.
7
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
1
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
.
.
.
9 _main()0
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
1
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
1

_main()5

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
8
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
4
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
0
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
1

_main()5

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
8
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
4
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
5
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
1

_main()5

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
50
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
51
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
1

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
6
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
7 _main()0

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
9
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
8
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
4_main()4
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
1

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
6
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
7
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
01

Bây giờ như bạn có thể thấy, hàm thứ hai được thực thi trong quá trình thực hiện hàm chạy (function_async ()). Vì vậy, đây là những điều cơ bản bây giờ, hãy để Lôi xem cách chạy hai chức năng Async mãi mãi.

Chạy hai hàm Async mãi mãi Python:

Phương pháp 1: Chỉ cần sử dụng vòng lặp thực sự trong hàm chính:

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
7
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
11

Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
0
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
1
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
2

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
15

Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
4

Output:

Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
.
.
.
.

Đầu raUsing while True loops for both functions and calling them using asyncio.ensure_future() and loop.run_forever() 

Lưu ý: .create_task () được sử dụng để chạy nhiều hàm async tại một thời điểm.ensure_future lets us execute a coroutine in the background, without explicitly waiting for it to finish.

Python3

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
4
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
5

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
6
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
7
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
8

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
9
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
0
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
1
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
2
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
3
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
4
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
.
.
.
44

Bây giờ như bạn có thể thấy, hàm thứ hai được thực thi trong quá trình thực hiện hàm chạy (function_async ()). Vì vậy, đây là những điều cơ bản bây giờ, hãy để Lôi xem cách chạy hai chức năng Async mãi mãi.

_main()5

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
8
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
4
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
5
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
1

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
7
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
.
.
.
7
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
1
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
.
.
.
9 _main()0
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
1
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
1

_main()5

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
8
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
4
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
0
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
1

_main()5

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
8
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
4
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
5
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
1

_main()5

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
50
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
51
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
1

async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
6
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
7 _main()0

Bây giờ như bạn có thể thấy, hàm thứ hai được thực thi trong quá trình thực hiện hàm chạy (function_async ()). Vì vậy, đây là những điều cơ bản bây giờ, hãy để Lôi xem cách chạy hai chức năng Async mãi mãi.

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
7
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
50
async def speak_async():
   for i in range(100):
       print("Hello I'm Abhishek, writer on GFG")
51
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
1

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
7
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
8
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great
HELLO WORLD
HELLO WORLD
4_main()4
Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
1

Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
0
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
1
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
2

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
17

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
18

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 
19

Output:

Hello, I'm Abhishek
GFG is Great
Hello, I'm Abhishek
GFG is Great

 HELLO WORLD 

Hello, I'm Abhishek
GFG is Great
.
.
.

Làm cách nào để chạy chức năng Python trong nền?

Chúng ta có thể định cấu hình một luồng daemon mới để thực hiện một chức năng tùy chỉnh sẽ thực hiện một tác vụ chạy dài, chẳng hạn như giám sát một tài nguyên hoặc dữ liệu. Ví dụ: chúng ta có thể xác định một hàm mới có tên boundation_task (). Sau đó, chúng ta có thể định cấu hình một luồng mới. Thể hiện trường thể để thực thi chức năng này thông qua đối số của Target Target.configure a new daemon thread to execute a custom function that will perform a long-running task, such as monitor a resource or data. For example we might define a new function named background_task(). Then, we can configure a new threading. Thread instance to execute this function via the “target” argument.

Làm cách nào để chạy một chức năng không đồng bộ trong Python?

Để chạy chức năng Async (Coroutine), bạn phải gọi nó bằng cách sử dụng vòng lặp sự kiện.Các vòng lặp sự kiện: Bạn có thể nghĩ về vòng lặp sự kiện là các chức năng để chạy các tác vụ và cuộc gọi lại không đồng bộ, thực hiện các hoạt động IO mạng và chạy các quy trình con.Ví dụ 1: Vòng lặp sự kiện Ví dụ để chạy chức năng Async để chạy một hàm Async duy nhất: Python3.call it using an Event Loop. Event Loops: You can think of Event Loop as functions to run asynchronous tasks and callbacks, perform network IO operations, and run subprocesses. Example 1: Event Loop example to run async Function to run a single async function: Python3.

Làm thế nào để bạn gọi chức năng async mà không chờ đợi python?

Làm thế nào để tôi chạy Async mà không đang chờ đợi trong Python ?..
nhập khẩu asyncio ..
async def main ():.
in ('Xin chào').
chờ đợi asyncio.Ngủ (1).
In ('Thế giới!').
# Python 3.7+.

Làm thế nào để bạn chờ đợi chức năng async trong python?

Hàm Async sử dụng từ khóa đang chờ đợi để biểu thị một coroutine.Khi sử dụng từ khóa đang chờ, Coroutines giải phóng luồng điều khiển trở lại vòng lặp sự kiện.Để chạy một coroutine, chúng ta cần lên lịch cho vòng lặp sự kiện.Sau khi lên lịch, các coroutines được gói trong các nhiệm vụ như một đối tượng trong tương lai.. When using the await keyword, coroutines release the flow of control back to the event loop. To run a coroutine, we need to schedule it on the event loop. After scheduling, coroutines are wrapped in Tasks as a Future object.