简体   繁体   中英

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.

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

thread1 = threading.Thread(target=procedure1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM