python _thread问题

使用这套代码,输出的结果是a和b参数混在一起的

img

但是我想要的结果如下:
1 a
2 aa

我该怎么修改呢

import random
import _thread as thread
from time import sleep
def fun(a,b):
    print(a,b)
    sleep(random.randint(1,5))

for i in range(4):
    thread.start_new_thread(fun,(i+1,'a'*(i+1)))

input()

请采纳 已给你找到原因。请采纳:
print(a,b)换成 print(str(a)+' '+str(b))
因为你的写法是打印a再打印出b,在这期间,由于多线程的原因,可能在打印出a的值后,另一个线程也打印它的a的值,所以会出现你的问题,改成我写的后,表示把两个参数的值作为一个东西打印出来,这样就不会存在你的这个问题了。

多线程并发运行,快的很谁知道哪个线程先运行。

import random
import _thread as thread
import time
from time import sleep


def fun(a, b):
    print(a, b)
    sleep(random.randint(1, 5))


for i in range(4):
    thread.start_new_thread(fun, (i + 1, 'a' * (i + 1)))
    time.sleep(1)