Search code examples
pythonpython-3.xpython-module

How to use python threading


I want to be able to have two procedures run at the same time, current code below;

import time,threading

def procedure1():
    for i in range(0,5):
        time.sleep(1)
        print('hello')

def procedure2():
    for j in range(0,10):
        time.sleep(1)
        print(j)

thread1=procedure1()
thread2=procedure2()

thread1.start()
thread2.start()

However this makes the two procedures run one after the other rather than parallel as I will require. Just need this example completed to work and will be greatly appreciated.

Thanks in advance.


Solution

  • You import threading but don't use it. Try:

    thread1 = threading.Thread(target=procedure1)