kod start.py jest jak poniżej.ten sam program, inne wyjście w module wątków
import threading
class myThread(threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
def run(self):
currentThreadname = threading.currentThread()
print "running in ", currentThreadname
thread = myThread(1,"mythrd")
thread.start()
Uruchom go z pythonem dwa razy.
python start.py
running in <myThread(mythrd, started 140461133485824)>
python start.py
running in <myThread(mythrd, started 140122860668672)>
kod run.py wygląda jak poniżej.
import threading
class myThread(threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
def run(self):
currentThreadname = threading.currentThread()
print "running in ", currentThreadname
thread = myThread(1,"mythrd")
thread.run()
run.py to tylko jedna linia inna niż start.py.
Teraz uruchom program run.py na dwa razy.
python run.py
running in <_MainThread(MainThread, started 139854546364160)>
python run.py
running in <_MainThread(MainThread, started 139854546364160)>
Kod startandrun.py wygląda jak poniżej.
class myThread(threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
def run(self):
currentThreadname = threading.currentThread()
print "running in ", currentThreadname
thread = myThread(1,"mythrd")
thread.start()
thread.run()
Teraz uruchom również startandrun.py również dwa razy.
python startandrun.py
running in <myThread(mythrd, started 140317119899392)>
running in <_MainThread(MainThread, started 140317144454912)>
python startandrun.py
running in running in <_MainThread(MainThread, started 139980210505472)>
<myThread(mythrd, started 139980185949952)>
Jak JohanL powiedzieć:
Uruchamiając dwa oddzielne wątki, wszystkie zakłady są wyłączone, co do których zostanie wykonany pierwszy.
Jesteś zasadniczo pozostawiając harmonogramu do systemu operacyjnego. po raz pierwszy wykonać startandrun.py, thread.start()
został wykonany przed thread.run()
to spowodować wyjście:
running in <myThread(mythrd, started 140317119899392)>
running in <_MainThread(MainThread, started 140317144454912)>
Drugi raz wykonać startandrun.py, thread.start()
został stracony po thread.run()
, dlaczego nie doprowadzić do wyjścia :
running in <_MainThread(MainThread, started 140317144454912)>
running in <myThread(mythrd, started 140317119899392)>
zamiast
running in running in <_MainThread(MainThread, started 139980210505472)>
<myThread(mythrd, started 139980185949952)>
Uruchamiając dwa oddzielne wątki, wszystkie zakłady są wyłączone, aby który zostanie wykonany jako pierwszy. Zasadniczo opuszczasz planowanie w systemie operacyjnym. Dlatego potrzebne są różne prymitywy synchronizacji (semafory, monitory et.c.), gdy twoje wątki uzyskują dostęp do tych samych danych. – JohanL
'thread.run' nie uruchamia nowego wątku, więc ten kod prawdopodobnie nie robi tego, co myślisz, że robi. Aby rozpocząć wątek, musisz wywołać 'start'. Nie powinieneś nazywać 'run' bezpośrednio,' start' zrobi to za ciebie. Wywoływanie początku i końca służy tylko dalszemu pomyleniu każdego problemu, który próbujesz zrozumieć. Zobacz dokumentację, która jest dość jasna na temat tego: "Gdy obiekt wątku zostanie utworzony, jego aktywność musi zostać uruchomiona przez wywołanie metody start() wątku. Wywołuje to metodę run() w osobnym wątku kontroli." – pvg