TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to PYTHON

Python asyncio Example: yield from asyncio.sleep

Use the asyncio module to run coroutines in parallel. Review ensure_future and yield from asyncio.sleep.
Asyncio. Often methods need to run, but we do not need to wait for them. They are not blocking methods. We can run them in the background.
With asyncio, a module in Python 3.5, we can use an event loop to run asynchronous methods. With "yield from" we can run methods in parallel.
An example. This program introduces a simple "logic" method that computes a number. After each iteration it uses the "yield from" syntax to call asynio.sleep.

Asyncio: We use get_event_loop to begin adding methods to run. We create a tasks list with ensure_future calls.

Run: We call run_until_complete with the result of gather() to execute all our methods in parallel.

Sleep: The methods would not yield to each other without the "yield from asyncio.sleep" statement.

Python program that uses asyncio import asyncio @asyncio.coroutine def logic(max): # This method runs some logic in a loop. # ... The max is specified as an argument. count = 0 for i in range(1, max): count += i count = count / i # Provide a chance to run other methods. yield from asyncio.sleep(1) # Finished. print("Logic result", max, count) # Get our event loop. loop = asyncio.get_event_loop() # Call logic method four times. tasks = [ asyncio.ensure_future(logic(5)), asyncio.ensure_future(logic(20)), asyncio.ensure_future(logic(10)), asyncio.ensure_future(logic(1))] # Run until all logic methods have completed. # ... The sleep call will allow all to run in parallel. loop.run_until_complete(asyncio.gather(*tasks)) loop.close() Output Logic result 1 0 Logic result 5 1.375 Logic result 10 1.1274057539682538 Logic result 20 1.0557390762436003
Yield. Having a call to a "yield from" method is critical to having parallel method execution in Python. Sleep() simply does nothing—it pauses the current thread.

But: Having asleep call gives other methods a chance to run. The other methods run when asyncio.sleep is called.

Some notes. In a real program, the asyncio.sleep method is still useful. In a long-running method, we can call asyncio.sleep periodically to allow other things to happen.
Some notes, continued. With the basic pattern in this program, we can run tasks in parallel in Python. We can load data from files, compute values in memory, or do anything.
Official documentation. With the asyncio module introduced in Python 3.5, we can access many built-in asyncio methods. For complete coverage, please use the Python documentation.Tasks and coroutines: Python.org
A review. Async programming is a key development in Python 3.5. This feature enables more complex programs to execute—without blocking. So the program remains responsive.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf