How do I run two python loops concurrently?

PythonProgrammingServer Side Programming

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.

raja
Published on 20-Apr-2018 10:21:35
Advertisements