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)>
答案 0 :(得分:4)
由于您打印值的方式,这种情况正在发生:
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
答案 1 :(得分:2)
所以,你想要的只是同步你的线程。可以使用线程库中的 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()
您还可以使用信号量和锁定以获得更好的理由。有关更多详细信息,请参阅文档。
答案 2 :(得分:2)
可能你不明白线程是如何工作的。 仔细阅读this。
我强烈建议您使用g
库中的PrevNode
。
答案 3 :(得分:0)
你使用的是什么版本的python?在python 2中,&#34; print&#34;不是线程安全的。请参阅http://tech.queryhome.com/54593/is-print-thread-safe-in-python-2-6-2-7。
如果线程在&#34; print&#34;期间切换,输出会混合,就像你看到的那样。