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

1、thread方法啟動(dòng)了新的線程,并返回了它的識(shí)別符。
該系統(tǒng)將使用傳輸?shù)膮?shù)列表調(diào)用指定為函數(shù)參數(shù)的函數(shù)。 function 返回時(shí)線程會(huì)靜默退出。
2、Args是參數(shù)元組,使用空元組調(diào)用function不帶參數(shù)。
可選參數(shù)指定關(guān)鍵詞參數(shù)的字典。
#語(yǔ)法 thread.start_new_thread ( function, args[, kwargs] )
實(shí)例
#python 多線程示例。
#1. 使用遞歸計(jì)算階乘。
#2. 使用線程調(diào)用階乘函數(shù)。
from _thread import start_new_thread
from time import sleep
threadId = 1 #線程計(jì)數(shù)器
waiting = 2 #2秒等待的時(shí)間
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)建線程,希望對(duì)大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)Python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
網(wǎng)站欄目:創(chuàng)新互聯(lián)Python教程:pythonthread模塊創(chuàng)建線程
本文URL:http://www.dlmjj.cn/article/ccccpcg.html


咨詢(xún)
建站咨詢(xún)
