多线程 VS 多进程
程序:一堆代码以文本形式存入一个文档 进程:程序运行的一个状态
包含地址空间,内存,数据栈 每个进程有自己完全独立的运行环境,多进程共享数据是一个问题 线程
一个进程的独立运行片段,一个进程可以有多个线程 轻量化的进程 一个进程的多个线程间共享数据和上下文运行环境 共享互斥问题 全局解释器锁(GIL)
python代码的执行是由python虚拟机进行控制 在主循环中只能有一个控制线程在执行 python包
thread:有问题,不好用,python3版本改成了_thread threading:通行的包
'''
利用time函数,生成两个函数
顺序调用
计算总的运行时间
'''
import time
def loop1 ( ) :
print ( 'Start loop 1 at :' , time. ctime( ) )
time. sleep( 4 )
print ( 'End loop 1 at:' , time. ctime( ) )
def loop2 ( ) :
print ( 'Start loop 2 at :' , time. ctime( ) )
time. sleep( 2 )
print ( 'End loop 2 at:' , time. ctime( ) )
def main ( ) :
print ( "Starting at:" , time. ctime( ) )
loop1( )
loop2( )
print ( "All done at:" , time. ctime( ) )
if __name__ == '__main__' :
main( )
Starting at: Tue Jan 1 11:08:36 2019
Start loop 1 at : Tue Jan 1 11:08:36 2019
End loop 1 at: Tue Jan 1 11:08:40 2019
Start loop 2 at : Tue Jan 1 11:08:40 2019
End loop 2 at: Tue Jan 1 11:08:42 2019
All done at: Tue Jan 1 11:08:42 2019
'''
利用time函数,生成两个函数
顺序调用
计算总的运行时间
使用多线程,缩短总时间,使用_thread
'''
import time
import _thread as thread
def loop1 ( ) :
print ( 'Start loop 1 at :' , time. ctime( ) )
time. sleep( 4 )
print ( 'End loop 1 at:' , time. ctime( ) )
def loop2 ( ) :
print ( 'Start loop 2 at :' , time. ctime( ) )
time. sleep( 2 )
print ( 'End loop 2 at:' , time. ctime( ) )
def main ( ) :
print ( "Starting at:" , time. ctime( ) )
thread. start_new_thread( loop1, ( ) )
thread. start_new_thread( loop2, ( ) )
print ( "All done at:" , time. ctime( ) )
if __name__ == '__main__' :
main( )
while True :
time. sleep( 1 )
Starting at: Tue Jan 1 11:15:30 2019
All done at: Tue Jan 1 11:15:30 2019
Start loop 1 at : Tue Jan 1 11:15:30 2019
Start loop 2 at : Tue Jan 1 11:15:30 2019
End loop 2 at: Tue Jan 1 11:15:32 2019
End loop 1 at: Tue Jan 1 11:15:34 2019
import time
import _thread as thread
def loop1 ( in1) :
print ( 'Start loop 1 at :' , time. ctime( ) )
print ( "我是参数 " , in1)
time. sleep( 4 )
print ( 'End loop 1 at:' , time. ctime( ) )
def loop2 ( in1, in2) :
print ( 'Start loop 2 at :' , time. ctime( ) )
print ( "我是参数 " , in1 , "和参数 " , in2)
time. sleep( 2 )
print ( 'End loop 2 at:' , time. ctime( ) )
def main ( ) :
print ( "Starting at:" , time. ctime( ) )
thread. start_new_thread( loop1, ( "王老大" , ) )
thread. start_new_thread( loop2, ( "王大鹏" , "王晓鹏" ) )
print ( "All done at:" , time. ctime( ) )
if __name__ == "__main__" :
main( )
while True :
time. sleep( 10 )
threading的使用
直接使用threading.Thread生成Thread实例
1.t = threading.Thread(target = xxx, args=(xxx, )) #target是函数名,args是参数 2.t.start() #气动多线程 3.t.join():等待多线程执行完成 守护线程,deamon
如果在程序中将子线程设置成守护线程,则子线程会在主线程结束的时候自动退出 一般认为,守护线程不重要或者不允许离开主线程独立运行 守护线程案例能否有效果跟环境相关 线程常用属性
threading.currentThread:返回当前线程变量 threading.enumerate:返回一个包含正在运行的线程的list,正在运行的线程指的是线程启动后,结束前的线程 threading.ativeCount:返回正在运行的线程数量,效果跟len(threading.enumerate)相同 thr.setName:给线程设置名字 thr.getName:得到线程的名字
import time
import threading
def loop1 ( in1) :
print ( 'Start loop 1 at :' , time. ctime( ) )
print ( "我是参数 " , in1)
time. sleep( 4 )
print ( 'End loop 1 at:' , time. ctime( ) )
def loop2 ( in1, in2) :
print ( 'Start loop 2 at :' , time. ctime( ) )
print ( "我是参数 " , in1 , "和参数 " , in2)
time. sleep( 2 )
print ( 'End loop 2 at:' , time. ctime( ) )
def main ( ) :
print ( "Starting at:" , time. ctime( ) )
t1 = threading. Thread( target= loop1, args= ( "王老大" , ) )
t1. start( )
t2 = threading. Thread( target= loop2, args= ( "王大鹏" , "王小鹏" ) )
t2. start( )
print ( "All done at:" , time. ctime( ) )
if __name__ == "__main__" :
main( )
while True :
time. sleep( 10 )
import time
import threading
def loop1 ( in1) :
print ( 'Start loop 1 at :' , time. ctime( ) )
print ( "我是参数 " , in1)
time. sleep( 4 )
print ( 'End loop 1 at:' , time. ctime( ) )
def loop2 ( in1, in2) :
print ( 'Start loop 2 at :' , time. ctime( ) )
print ( "我是参数 " , in1 , "和参数 " , in2)
time. sleep( 2 )
print ( 'End loop 2 at:' , time. ctime( ) )
def main ( ) :
print ( "Starting at:" , time. ctime( ) )
t1 = threading. Thread( target= loop1, args= ( "王老大" , ) )
t1. start( )
t2 = threading. Thread( target= loop2, args= ( "王大鹏" , "王小鹏" ) )
t2. start( )
t1. join( )
t2. join( )
print ( "All done at:" , time. ctime( ) )
if __name__ == "__main__" :
main( )
while True :
time. sleep( 10 )
import time
import threading
def fun ( ) :
print ( "Start fun" )
time. sleep( 2 )
print ( "end fun" )
print ( "Main thread" )
t1 = threading. Thread( target= fun, args= ( ) )
t1. start( )
time. sleep( 1 )
print ( "Main thread end" )
import time
import threading
def fun ( ) :
print ( "Start fun" )
time. sleep( 2 )
print ( "end fun" )
print ( "Main thread" )
t1 = threading. Thread( target= fun, args= ( ) )
t1. setDaemon( True )
t1. start( )
time. sleep( 1 )
print ( "Main thread end" )
import time
import threading
def loop1 ( ) :
print ( 'Start loop 1 at :' , time. ctime( ) )
time. sleep( 6 )
print ( 'End loop 1 at:' , time. ctime( ) )
def loop2 ( ) :
print ( 'Start loop 2 at :' , time. ctime( ) )
time. sleep( 1 )
print ( 'End loop 2 at:' , time. ctime( ) )
def loop3 ( ) :
print ( 'Start loop 3 at :' , time. ctime( ) )
time. sleep( 5 )
print ( 'End loop 3 at:' , time. ctime( ) )
def main ( ) :
print ( "Starting at:" , time. ctime( ) )
t1 = threading. Thread( target= loop1, args= ( ) )
t1. setName( "THR_1" )
t1. start( )
t2 = threading. Thread( target= loop2, args= ( ) )
t2. setName( "THR_2" )
t2. start( )
t3 = threading. Thread( target= loop3, args= ( ) )
t3. setName( "THR_3" )
t3. start( )
time. sleep( 3 )
for thr in threading. enumerate ( ) :
print ( "正在运行的线程名字是: {0}" . format ( thr. getName( ) ) )
print ( "正在运行的子线程数量为: {0}" . format ( threading. activeCount( ) ) )
print ( "All done at:" , time. ctime( ) )
if __name__ == "__main__" :
main( )
while True :
time. sleep( 10 )
import threading
import time
class MyThread ( threading. Thread) :
def __init__ ( self, arg) :
super ( MyThread, self) . __init__( )
self. arg = arg
def run ( self) :
time. sleep( 2 )
print ( "The args for this class is {0}" . format ( self. arg) )
for i in range ( 5 ) :
t = MyThread( i)
t. start( )
t. join( )
print ( "Main thread is done!!!!!!!!" )
import threading
from time import sleep, ctime
loop = [ 4 , 2 ]
class ThreadFunc :
def __init__ ( self, name) :
self. name = name
def loop ( self, nloop, nsec) :
'''
:param nloop: loop函数的名称
:param nsec: 系统休眠时间
:return:
'''
print ( 'Start loop ' , nloop, 'at ' , ctime( ) )
sleep( nsec)
print ( 'Done loop ' , nloop, ' at ' , ctime( ) )
def main ( ) :
print ( "Starting at: " , ctime( ) )
t = ThreadFunc( "loop" )
t1 = threading. Thread( target = t. loop, args= ( "LOOP1" , 4 ) )
t2 = threading. Thread( target = ThreadFunc( 'loop' ) . loop, args= ( "LOOP2" , 2 ) )
t1. start( )
t2. start( )
t1. join( )
t2. join( )
print ( "ALL done at: " , ctime( ) )
if __name__ == '__main__' :
main( )
112