Perform multiple tasks concurrently in Python: “Asynchronous Programming in Python with Asyncio”

Asyncio is a Python library for writing asynchronous code. Asynchronous programming allows a program to perform multiple tasks concurrently, rather than waiting for one task to be complete before starting the next. This can be useful for improving the …


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Anurag Verma

Asyncio is a Python library for writing asynchronous code. Asynchronous programming allows a program to perform multiple tasks concurrently, rather than waiting for one task to be complete before starting the next. This can be useful for improving the performance of programs that perform tasks that take a long time to complete, such as making network requests or reading and writing to a database.

Asyncio uses the async/await syntax to define asynchronous functions and to pause and resume them as needed. Here's a simple example of an asynchronous function that uses asyncio to pause and wait for a delay:

import asyncio

async def say_after(delay, what):
    await asyncio.sleep(delay)
    print(what)

async def main():
    print("started")
    await say_after(1, "hello")
    await say_after(2, "world")
    print("completed")

asyncio.run(main())

This code will print "started", then "hello" after a delay of 1 second, followed by "world" after a delay of 2 seconds. Finally, it will print "completed". Asyncio allows the program to perform other tasks while waiting for the delays to complete, rather than blocking until they are finished.

I hope you liked it!


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Anurag Verma


Print Share Comment Cite Upload Translate Updates
APA

Anurag Verma | Sciencx (2022-12-27T17:54:25+00:00) Perform multiple tasks concurrently in Python: “Asynchronous Programming in Python with Asyncio”. Retrieved from https://www.scien.cx/2022/12/27/perform-multiple-tasks-concurrently-in-python-asynchronous-programming-in-python-with-asyncio/

MLA
" » Perform multiple tasks concurrently in Python: “Asynchronous Programming in Python with Asyncio”." Anurag Verma | Sciencx - Tuesday December 27, 2022, https://www.scien.cx/2022/12/27/perform-multiple-tasks-concurrently-in-python-asynchronous-programming-in-python-with-asyncio/
HARVARD
Anurag Verma | Sciencx Tuesday December 27, 2022 » Perform multiple tasks concurrently in Python: “Asynchronous Programming in Python with Asyncio”., viewed ,<https://www.scien.cx/2022/12/27/perform-multiple-tasks-concurrently-in-python-asynchronous-programming-in-python-with-asyncio/>
VANCOUVER
Anurag Verma | Sciencx - » Perform multiple tasks concurrently in Python: “Asynchronous Programming in Python with Asyncio”. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/12/27/perform-multiple-tasks-concurrently-in-python-asynchronous-programming-in-python-with-asyncio/
CHICAGO
" » Perform multiple tasks concurrently in Python: “Asynchronous Programming in Python with Asyncio”." Anurag Verma | Sciencx - Accessed . https://www.scien.cx/2022/12/27/perform-multiple-tasks-concurrently-in-python-asynchronous-programming-in-python-with-asyncio/
IEEE
" » Perform multiple tasks concurrently in Python: “Asynchronous Programming in Python with Asyncio”." Anurag Verma | Sciencx [Online]. Available: https://www.scien.cx/2022/12/27/perform-multiple-tasks-concurrently-in-python-asynchronous-programming-in-python-with-asyncio/. [Accessed: ]
rf:citation
» Perform multiple tasks concurrently in Python: “Asynchronous Programming in Python with Asyncio” | Anurag Verma | Sciencx | https://www.scien.cx/2022/12/27/perform-multiple-tasks-concurrently-in-python-asynchronous-programming-in-python-with-asyncio/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.