import threading
import time
def test1():
print "hello"
time.sleep(15)
print "hello2"
def test2():
print "hi"
th1 =threading.Thread(test1())
th2 =threading.Thread(test2())
p=[th1,th2]
for i in p:
i.start()
for i in p:
i.join()
내가 옳은지 잘 모르겠습니다. 그렇지 않으면 수정 해주세요. 출력이 hello hi 및 hello2 순서로 인쇄 될 것으로 예상합니다. 두 개의 스레드가 병렬로 실행될 것으로 예상하고 있습니다. 하지만 아래 출력, hello hello2 및 hi가 표시됩니다. Thread2는 thread1이 완료된 후에 만 실행됩니다. 내가 뭘 잘못하고 있니? 또는 내 이해 또는 스레딩이 잘못 되었습니까?
( 기본값 :)Thread()
이라는 "키워드 인수" 를 사용하는 클래스 생성자에 함수 참조를 전달해야합니다 .target
None
함수 호출의 결과 ( 예 : 수행 한 작업 )를 전달하면 특히에 대한 첫 번째 인수 Thread()
가 실제로 이므로 원하지 않는 동작 이 발생 group=None
합니다.
예 : ( 수정 됨 )
import threading
import time
def test1():
print "hello"
time.sleep(15)
print "hello2"
def test2():
print "hi"
th1 = threading.Thread(target=test1)
th2 = threading.Thread(target=test2)
p = [th1, th2]
for i in p:
i.start()
for i in p:
i.join()
산출:
$ python foo.py
hello
hi
# 15s delay
hello2
구체적으로 특별히:
class threading.Thread(group=None, target=None, name=None, args=(), kwargs={})
이 생성자는 항상 키워드 인수로 호출해야합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다