簡體   English   中英

同一程序在線程模塊中輸出不同的輸出

[英]same program different output in threading module

start.py代碼如下。

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()

用python啟動它兩次。

python start.py
running in  <myThread(mythrd, started 140461133485824)>
python start.py
running in  <myThread(mythrd, started 140122860668672)>

run.py代碼如下。

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只有一行不同於start.py。
現在啟動run.py兩次。

python  run.py
running in  <_MainThread(MainThread, started 139854546364160)>
python  run.py
running in  <_MainThread(MainThread, started 139854546364160)>

startandrun.py代碼如下。

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()

現在也開始startandrun.py兩次。

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)>

正如JohanL所說:
當運行兩個單獨的線程時,所有的注意都將關閉,因為它將首先執行。
您基本上將調度留給操作系統。 第一次執行startandrun.py時, thread.start()thread.run()之前執行,它導致輸出:

running in  <myThread(mythrd, started 140317119899392)>
running in  <_MainThread(MainThread, started 140317144454912)>

第二次執行startandrun.py, thread.start()thread.run()之后執行,為什么不導致輸出:

running in  <_MainThread(MainThread, started 140317144454912)>
running in  <myThread(mythrd, started 140317119899392)>

代替

running in running in  <_MainThread(MainThread, started 139980210505472)>
 <myThread(mythrd, started 139980185949952)>

這是因為您打印值的方式:

print "running in ", currentThreadname

添加逗號類似於:

print 'running in ' # without new line at the end
print currentThreadname

由於這兩個函數同時運行,因此訂單的執行方式如下:

print 'running in ' # without new line FUNCTION #1
print 'running in ' # without new line FUNCTION #2
print currentThreadName # with new line at the end FUNCTION #1
print currentThreadName # with new line at the end FUNCTION #2

嘗試使用一個沒有逗號的print語句來理解它應該是什么:

def run(self):
    currentThreadname = threading.currentThread()
    print "running in {}".format(currentThreadname)

這將表現正常,但由於這兩個函數同時打印,您可能會得到以下輸出:

running in <myThread(mythrd, started 10716)>running in <_MainThread(MainThread, started 12132)>

因此,要證明這將起作用,您可以使用time.sleep()在兩次調用之間使用延遲:

import threading
import time

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 {}".format(currentThreadname)

thread = myThread(1,"mythrd")
thread.start()
time.sleep(0.1)
thread.run()

現在你可以看到你得到了你想要的輸出,因為每個函數都打印一次,在兩次調用之間延遲0.1秒:

running in <myThread(mythrd, started 5600)>
running in <_MainThread(MainThread, started 7716)>

編輯:

您的問題正是為什么您應該使用多線程而不是兩次運行相同的線程。 當你使用多線程你可以使用thread.join() ,它將等待線程完成然后繼續代碼,或者你可以使用threading.lock()這樣你可以繼續你的代碼,但鎖定一個函數使用一次一個線程。 這里有些例子:

thread.join()

thread = myThread(1, "mythrd")
thread2 = myThread(2, "thrd2")
thread.start()
thread.join() # code will stop here and wait for thread to finish then continue
thread2.run()

threading.lock()

....
    def run(self):
        with lock: # if one thread uses this lock the other threads have to wait
            currentThreadname = threading.currentThread()
            print "running in ", currentThreadname

thread = myThread(1, "mythrd")
thread2 = myThread(2, "thrd2")
lock = threading.Lock()
thread.start() 
thread2.run()
# code keeps running even if there are threads waiting for the lock

所以,你想要的只是同步你的線程。 可以使用線程庫中的join()函數輕松完成。

你可以做這樣的事情

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")
t1 =  thread.start()
t1.join()
t2 =  thread.run()
t2.join()

您還可以使用信號量和鎖定以獲得更好的理由。 有關更多詳細信息,請參閱文檔。

可能你不明白線程是如何工作的。 閱讀仔細。

我強烈建議您使用futures庫中的ThreadPoolExecutor

你用的是什么版本的python? 在python 2中,“print”不是線程安全的。 請參閱http://tech.queryhome.com/54593/is-print-thread-safe-in-python-2-6-2-7

如果線程在“打印”期間切換,則輸出會混合,就像您看到的那樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM