Run Two Python Loops Concurrently



You will need to use a multiprocessing library. You will need to spawn a new process and provide the code to it as an argument. For example,

from multiprocessing import Process

def loop_a():
   for i in range(5):
      print("a")

def loop_b():
   for i in range(5):
      print("b")

Process(target=loop_a).start()
Process(target=loop_b).start()

This might process different outputs at different times. This is because we don't know which print will be executed when.

Updated on: 2020-06-17T12:22:33+05:30

792 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements