一. threading的引數傳遞,引數之後的』,『不能少,此處的』,『是用來區分此引數作為元組(包含多個引數)來傳遞的,而不是單個引數傳遞
#coding:utf-8import threading
import time
def b(arg):
time.sleep(2)
print(
"i'm b")
print(
"i'm
", threading.current_thread().name)
print(
"i'm
",arg)
time.sleep(2)
print(
"i'm sleep after")
def a():
print(
"i'm a")
s = threading.thread(target=b, name='
loopthread
',args=("test"
,)) s.start()
print(
"i'm
",threading.current_thread().name)
#time.sleep(3)
print(
"i'm c")
print(a())
二. sleep的位置很重要,如上輸出結果:
i'm ai'm mainthread
i'm c
none
i'm b
i'm loopthread
i'm test
i'm sleep after
第二種情況:
#coding:utf-8import threading
import time
def b(arg):
#time.sleep(2)
print(
"i'm b")
print(
"i'm
", threading.current_thread().name)
print(
"i'm
",arg)
time.sleep(2)
print(
"i'm sleep after")
def a():
print(
"i'm a")
s = threading.thread(target=b, name='
loopthread
',args=("
test
",))
s.start()
print(
"i'm
",threading.current_thread().name)
#time.sleep(3)
print(
"i'm c")
print(a())
輸出結果:
i'm ai'm bi'm
i'm loopthread
i'm test
mainthread
i'm c
none
i'm sleep after
第三種情況:
#coding:utf-8import threading
import time
def b(arg):
#time.sleep(2)
print(
"i'm b")
print(
"i'm
", threading.current_thread().name)
print(
"i'm
",arg)
time.sleep(2)
print(
"i'm sleep after")
def a():
print(
"i'm a")
s = threading.thread(target=b, name='
loopthread
',args=("
test
",))
s.start()
print(
"i'm
",threading.current_thread().name)
time.sleep(3)
print(
"i'm c")
print(a())
輸出結果:
i'm ai'm bi'm
mainthread
i'm loopthread
i'm test
i'm sleep after
i'm c
none
似乎其中乙個執行緒稍微阻塞(或睡眠)就會立馬開始執行另外乙個,可以據此調控兩個執行緒的交替?
關於執行緒的引數傳遞需要注意
python 多執行緒thread
python通過thread模組支援多執行緒,語法也很簡潔,現在通過乙個例項來看一下python中的多執行緒 import thread import time 保證只額外啟動乙個執行緒 isrunning false 啟動的時間控制,測試時間是23點44分,所以定的是這個時間,可以用於指定定時任務...
Python多執行緒Thread
import threading import time import random def worker name print name 開始執行.n 0 while true print name 輸出 str n n n 1 t random.randint 0,5 print name 休眠...
python多執行緒使用thread
import sched import threading import time defnew task function,delay time,args 定時任務函式 param function 需要執行的函式 param delay time 延遲多少時間執行 param args 需要給f...