新聞中心
這里有您想知道的互聯網營銷解決方案
創(chuàng)新互聯Python教程:pythonthread模塊創(chuàng)建線程
thread方法對創(chuàng)建線程有效且直接。您可以在Linux和Windows中運行程序。

1、thread方法啟動了新的線程,并返回了它的識別符。
該系統(tǒng)將使用傳輸的參數列表調用指定為函數參數的函數。 function 返回時線程會靜默退出。
2、Args是參數元組,使用空元組調用function不帶參數。
可選參數指定關鍵詞參數的字典。
#語法 thread.start_new_thread ( function, args[, kwargs] )
實例
#python 多線程示例。
#1. 使用遞歸計算階乘。
#2. 使用線程調用階乘函數。
from _thread import start_new_thread
from time import sleep
threadId = 1 #線程計數器
waiting = 2 #2秒等待的時間
def factorial(n):
global threadId
rc = 0
if n < 1: # base case
print("{}: {}".format('\nThread', threadId ))
threadId += 1
rc = 1
else:
returnNumber = n * factorial( n - 1 ) # recursive call
print("{} != {}".format(str(n), str(returnNumber)))
rc = returnNumber
return rc
start_new_thread(factorial, (5, ))
start_new_thread(factorial, (4, ))
print("Waiting for threads to return...")
sleep(waiting)
以上就是python thread模塊創(chuàng)建線程,希望對大家有所幫助。更多Python學習指路:創(chuàng)新互聯Python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
網頁名稱:創(chuàng)新互聯Python教程:pythonthread模塊創(chuàng)建線程
文章起源:http://m.fisionsoft.com.cn/article/ccccpcg.html


咨詢
建站咨詢
