线程与进程-2

1. 主线程启动了子线程之后,主线程与子线程之间是相互独立的。默认情况下主线程是不会等待子线程执行完毕的,主线程只会按照自己的步调执行。

    所以在主线程中是没法测定子线程的运行时间的。

 

#主线程启动子线程之后,两者是并行的,相互之间是独立的。
import threading,time

def run(n):
    print('task',n)
    time.sleep(2)

start_time=time.time()

for i in range(10):
    t=threading.Thread(target=run,args=("t-%s"%i,))
    t.start()

print("cost time is:",time.time()-start_time)

 运行结果:

task t-0
task t-1
task t-2
task t-3
task t-4
task t-5
task t-6
task t-7
task t-8
task t-9
cost: 0.002000093460083008

Process finished with exit code 0

 

2. 用join()函数等待每个的执行结果,每个都执行完了再打印总用了的时间。 

 

#主线程启动子线程之后,两者是并行的,相互之间是独立的。
import threading,time

def run(n):
    print('task',n)
    time.sleep(2)

start_time=time.time()
t_objs=[] #存线程实例

for i in range(10):
    t=threading.Thread(target=run,args=("t-%s"%i,))
    t.start()
    t_objs.append(t) #为了不阻塞后面线程的启动,不在这里join,先放到一个列表里面。

for t in t_objs: #循环线程实例列表,等待所有线程执行完毕。
    t.join() #join=wait,意思是等t的运行结果


print("All threads have finished,cost time is:",time.time()-start_time)

 运行结果:

task t-0
task t-1
task t-2
task t-3
task t-4
task t-5
task t-6
task t-7
task t-8
task t-9
All threads have finished,cost time is: 2.0012001991271973

Process finished with exit code 0

3. 用 threading.current_thread 命令查看当前线程是主线程还是子线程。启用了10个线程,实际上有11个线程在执行(1个主线程+10个子线程)

#主线程启动子线程之后,两者是并行的,相互之间是独立的。
import threading,time

def run(n):
    print('task',n)
    time.sleep(2)
    print('task done',threading.current_thread()) #打印当前线程是主线程还是子线程

start_time=time.time()
t_objs=[]

for i in range(10):
    t=threading.Thread(target=run,args=("t-%s"%i,))
    t.start()
    t_objs.append(t)

for t in t_objs:
    t.join() #join=wait,意思是等t的运行结果

print('----All threads has finished',threading.current_thread())
print("Cost time is:",time.time()-start_time)

 运行结果:

task t-0
task t-1
task t-2
task t-3
task t-4
task t-5
task t-6
task t-7
task t-8
task t-9
task done <Thread(Thread-4, started 468)>
task done <Thread(Thread-1, started 7768)>
task done <Thread(Thread-5, started 11480)>
task done <Thread(Thread-3, started 11520)>
task done <Thread(Thread-6, started 7248)>
task done <Thread(Thread-2, started 6752)>
task done <Thread(Thread-7, started 2728)>
task done <Thread(Thread-10, started 10260)>
task done <Thread(Thread-8, started 12244)>
task done <Thread(Thread-9, started 2112)>
----All threads has finished <_MainThread(MainThread, started 11372)>
Cost time is: 2.002200126647949

Process finished with exit code 0

 4. 用threading.active_count() 来查看当前活跃的线程的个数。

#主线程启动子线程之后,两者是并行的,相互之间是独立的。
import threading,time

def run(n):
    print('task',n)
    time.sleep(2)
    print('task done',threading.current_thread()) #打印当前线程是主线程还是子线程

start_time=time.time()
t_objs=[]

for i in range(10):
    t=threading.Thread(target=run,args=("t-%s"%i,))
    t.start()
    t_objs.append(t)

#for t in t_objs:
#    t.join() #join=wait,意思是等t的运行结果

print('----All threads has finished',threading.current_thread(),threading.active_count())
print("Cost time is:",time.time()-start_time)

 运行结果:

task t-0
task t-1
task t-2
task t-3
task t-4
task t-5
task t-6
task t-7
task t-8
task t-9
----All threads has finished <_MainThread(MainThread, started 9096)> 11
Cost time is: 0.0010001659393310547
task done <Thread(Thread-1, started 10804)>
task done <Thread(Thread-2, started 11564)>
task done <Thread(Thread-3, started 11532)>
task done <Thread(Thread-4, started 10896)>
task done <Thread(Thread-7, started 1596)>
task done <Thread(Thread-6, started 11116)>
task done <Thread(Thread-8, started 10088)>
task done <Thread(Thread-9, started 10156)>
task done <Thread(Thread-10, started 10820)>
task done <Thread(Thread-5, started 12180)>

Process finished with exit code 0

 

 

 


 

 
 

 

posted on 2017-08-11 10:59  momo8238  阅读(157)  评论(0编辑  收藏  举报