擅长:python、mysql、java
<p>您可以使用<a href="https://docs.python.org/3/library/threading.html#thread-objects" rel="nofollow noreferrer">Thread</a>对象</p>
<pre><code>from threading import Thread
from time import sleep
from random import randint
def f(i):
sleep(randint(1, 3))
print(f'function called from Thread {i}')
for i in range(5):
Thread(target=f, args=(i,)).start()
</code></pre>
<p>和输出</p>
<pre><code>function called from Thread 4
function called from Thread 3
function called from Thread 1
function called from Thread 0
function called from Thread 2
</code></pre>