在Debian系統中實現Python多線程,你可以使用Python的內置模塊threading
python --version
或者,如果你使用的是Python 3:
python3 --version
創建一個Python腳本文件,例如multithreading_example.py。
在腳本中,導入threading模塊,并定義一個函數,該函數將在新線程中運行。例如:
import threading
def print_numbers():
for i in range(1, 11):
print(i)
start()方法啟動線程:thread = threading.Thread(target=print_numbers)
thread.start()
join()方法等待子線程完成:thread.join()
import threading
def print_numbers():
for i in range(1, 11):
print(i)
thread = threading.Thread(target=print_numbers)
thread.start()
thread.join()
python multithreading_example.py
或者,如果你使用的是Python 3:
python3 multithreading_example.py
這將啟動一個新線程,該線程將打印數字1到10,同時主線程將繼續執行。這就是在Debian系統中使用Python實現多線程的方法。