Python Event-Driven Programming

In this tutorial, you will learn exactly what Python event-driven programming means Event-driven programming is also called the Asynchronous principle,  which is used in Python and many other programming languages.

Event-driven programming focuses on events they are :

  • Python’s event-driven programming model resolves around the concept of event loop.
  • The working of the program depends upon events happening in the program, which means that in a program when an event loop starts the events will decide in which order and what will execute in the program.

Event-Driven programming

 

Python Module – Asyncio:

  • The main problem is that we either use parallel or serial models which take more time to execute the tasks.
  • The asyncio module provides great flexibility for writing concurrent code as a single thread in a program.

Event loop:

  • Event loop acts around the way during the execution of the whole program and keeps track of incoming and execution of events.
  • Event loop constantly monitors the task queue and runs the task.

While executing the event loop we can use the following syntax:

  • To print the text: asyncio.get_event_loop()
  • To stop the loop: loop.stop()
  • To return the current time to the user: time()
  • Create a new event loop: new_event_loop()
  • To set the current context executing from the program to loop: set_event_loop()
  • To run the loop until stop() method is used we use: run_forever()

Here is an example to understand clearly:

import asyncio //import module
def hello(loop):
    print("You are learning about Event handling in Python")
    loop.stop()
loop = asyncio.get_event_loop()
loop.call_soon(hello, loop)
loop.run_forever()
loop.close()

Output:
You are learning about Event handling in Python

Futures:

This is a class that represents the tasks that are not completed i.e. it allows you to track the status of asynchronous tasks.

Syntax: asyncio.Future

Example:

 

import asyncio
async def Mycode(future):
    await asyncio.sleep(2)
    future.set_result("You are learing Futures in Python")
loop = asyncio.get_event_loop()
future=asyncio.Future()
asyncio.ensure_future(Mycode(future))
try:
    loop.run_until_complete(future)
    print(future.result())
finally:
    loop.close()

Output:

You are learning Futures in Python

Coroutines:

  • Coroutines are used to pause or resume which allows the nonblocking execution of code.
  • We can even suspend a coroutine during program execution.

To implement the coroutines we have two ways. They are:

  1. async def function()
  2. @asyncio.coroutine decoder

1. async def function():

This method is used to implement coroutines under the asyncio module.

Example:

import asyncio
async def Mycode():
    print("This is a coroutine")
loop = asyncio.get_event_loop()
try:
    loop.run_until_complete(Mycode())
finally:
    loop.close()

Output:

This is a coroutine

2. @asyncio.coroutine decoder:

Example:

import asyncio
@asyncio.coroutine
def Mycode():
    print("This is a coroutine")
loop=asyncio.get_event_loop()
try:
    loop.run_until_complete(Mycode())
finally:
    loop.close()

Output:

This is a coroutine

Tasks:

These are used to schedule and this subclass is responsible for the execution of coroutines within an event loop in a parallel manner.

Syntax: asyncio.Task

Example:

import asyncio
async def Mycode():
    print("Hello")
async def Anothercode():
    print("User")
loop = asyncio.get_event_loop()
task1 = loop.create_task(Mycode())
task2 = loop.create_task(Anothercode())
loop.run_until_complete(asyncio.wait([task1, task2]))

Output:

Hello
User

 

 

 

 

 

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top