scr*_*apy 12 python multithreading
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()
Run Code Online (Sandbox Code Playgroud)
用python启动它两次.
python start.py
running in <myThread(mythrd, started 140461133485824)>
python start.py
running in <myThread(mythrd, started 140122860668672)>
Run Code Online (Sandbox Code Playgroud)
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 Code Online (Sandbox Code Playgroud)
run.py只有一行不同于start.py.
现在启动run.py两次.
python run.py
running in <_MainThread(MainThread, started 139854546364160)>
python run.py
running in <_MainThread(MainThread, started 139854546364160)>
Run Code Online (Sandbox Code Playgroud)
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()
Run Code Online (Sandbox Code Playgroud)
现在也开始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)>
Run Code Online (Sandbox Code Playgroud)
正如JohanL所说:
当运行两个单独的线程时,所有的注意都将关闭,因为它将首先执行.
您基本上将调度留给操作系统.第一次执行startandrun.py thread.start()
之前执行过thread.run()
,导致输出:
running in <myThread(mythrd, started 140317119899392)>
running in <_MainThread(MainThread, started 140317144454912)>
Run Code Online (Sandbox Code Playgroud)
第二次执行startandrun.py thread.start()
后执行了thread.run()
,为什么不执行输出:
running in <_MainThread(MainThread, started 140317144454912)>
running in <myThread(mythrd, started 140317119899392)>
Run Code Online (Sandbox Code Playgroud)
代替
running in running in <_MainThread(MainThread, started 139980210505472)>
<myThread(mythrd, started 139980185949952)>
Run Code Online (Sandbox Code Playgroud)
发生这种情况是因为您打印值的方式:
print "running in ", currentThreadname
Run Code Online (Sandbox Code Playgroud)
添加逗号类似于:
print 'running in ' # without new line at the end
print currentThreadname
Run Code Online (Sandbox Code Playgroud)
由于这两个函数同时运行,因此订单的执行方式如下:
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
Run Code Online (Sandbox Code Playgroud)
尝试使用一个不带逗号的打印语句来理解它应该如何:
def run(self):
currentThreadname = threading.currentThread()
print "running in {}".format(currentThreadname)
Run Code Online (Sandbox Code Playgroud)
这将表现正常,但由于两个函数同时打印,您可能会得到以下输出:
running in <myThread(mythrd, started 10716)>running in <_MainThread(MainThread, started 12132)>
Run Code Online (Sandbox Code Playgroud)
因此,为了证明这可行,您可以使用以下命令在两个调用之间使用延迟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()
Run Code Online (Sandbox Code Playgroud)
现在您可以看到获得了所需的输出,因为每个函数都打印一次,两次调用之间有 0.1 秒的延迟:
running in <myThread(mythrd, started 5600)>
running in <_MainThread(MainThread, started 7716)>
Run Code Online (Sandbox Code Playgroud)
编辑:
您的问题正是为什么您应该使用多线程而不是运行同一线程两次。当您使用多线程时,您可以使用thread.join()
which来等待线程完成然后继续代码,或者您可以使用threading.lock()
这样您可以继续您的代码但锁定一次由一个线程使用的函数。这里有些例子:
线程.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()
Run Code Online (Sandbox Code Playgroud)
线程.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
Run Code Online (Sandbox Code Playgroud)