![]()
# #from multiprocessing import Process
# from threading import Thread
# import time
# def task(name):
# print('%s is running'%name)
# time.sleep(3)
# if __name__ == '__main__':
# t=Thread(target=task,args=('egon',))
# #t=Process(target=task, args=('egon',))
# t.start()
# print('主线程')
#主线程代码运行完不能死 等啊。其他线程运行了在死 有可能会用到主线程里面东西
#方式2
from multiprocessing import Process
from threading import Thread
import time
class My(Thread):
def run(self):
print('%s is runninf'%self.name)
time.sleep(3)
if __name__ == '__main__':
t=My()
t.start()
print('主线程')
#看一看是不是在一个线程里面
from threading import Thread
import time ,os
def task():
print('%s is running'%os.getpid())
time.sleep(3)
if __name__ == '__main__':
t=Thread(target=task,)
t.start()
print('主线程',os.getpid())
进程之间是互相隔离的,不共享。需要借助第三方来完成共享(借助队列,管道,共享数据)
![]()
from threading import Thread
import time,os
x=1000
def task():
global x
x=0
if __name__ == '__main__':
t=Thread(target=task,)
t.start()
t.join()
print('主线程',x)
![]()
Thread实例对象的方法
# isAlive(): 返回线程是否活动的。
# getName(): 返回线程名。
# setName(): 设置线程名。
threading模块提供的一些方法:
# threading.currentThread(): 返回当前的线程变量。
# threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。
# threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。
from threading import Thread,current_thread,active_count,enumerate
import time,os
def task():
print('%s is running'%current_thread().name)
time.sleep(3)
if __name__ == '__main__':
t1=Thread(target=task,name='第一个线程')
t2=Thread(target=task,)
t3=Thread(target=task,)
t1.start()
t2.start()
t3.start()
print(t1.is_alive())
print(active_count())
print('主线程',current_thread().name)
![]()
![]()
from threading import Thread,current_thread
import time
def task():
print('%s is running' %current_thread().name)
time.sleep(3)
if __name__ == '__main__':
t1=Thread(target=task,name='第一个')
t1.daemon=True
t1.start()
print('主线程')
迷惑人的例子
from threading import Thread
import time
def foo():
print(123)
# time.sleep(10) #如果这个等的时间大于下面等的时间,就把不打印end123了
time.sleep(2) #如果这个等的时间小于下面等的时间,就把end123也打印了
print('end123')
def bar():
print(456)
# time.sleep(5)
time.sleep(10)
print('end456')
if __name__ == '__main__':
t1 = Thread(target=foo)
t2 = Thread(target=bar)
t1.daemon = True #主线程运行完了守护的那个还没有干掉,
# 主线程等非守护线程全都结束它才结束
t1.start()
t2.start()
print('main---------')
![]()
from threading import current_thread,Thread,Lock
import os,time
def task():
global n
print('%s is running' %current_thread().getName())
temp=n
time.sleep(0.5)
n=temp-1
if __name__ == '__main__':
n=100
lock=Lock()
threads=[]
start_time=time.time()
for i in range(100):
t=Thread(target=task)
threads.append(t)
t.start()
for t in threads:
t.join()
stop_time=time.time()
print('主:%s n:%s' %(stop_time-start_time,n))
'''
Thread-1 is running
Thread-2 is running
......
Thread-100 is running
主:0.5216062068939209 n:99
'''
#不加锁:未加锁部分并发执行,加锁部分串行执行,速度慢,数据安全
from threading import current_thread,Thread,Lock
import os,time
def task():
#未加锁的代码并发运行
time.sleep(3)
print('%s start to run' %current_thread().getName())
global n
#加锁的代码串行运行
lock.acquire()
temp=n
time.sleep(0.5)
n=temp-1
lock.release()
if __name__ == '__main__':
n=100
lock=Lock()
threads=[]
start_time=time.time()
for i in range(100):
t=Thread(target=task)
threads.append(t)
t.start()
for t in threads:
t.join()
stop_time=time.time()
print('主:%s n:%s' %(stop_time-start_time,n))
'''
Thread-1 is running
Thread-2 is running
......
Thread-100 is running
主:53.294203758239746 n:0
'''
#有的同学可能有疑问:既然加锁会让运行变成串行,那么我在start之后立即使用join,就不用加锁了啊,也是串行的效果啊
#没错:在start之后立刻使用jion,肯定会将100个任务的执行变成串行,毫无疑问,最终n的结果也肯定是0,是安全的,但问题是
#start后立即join:任务内的所有代码都是串行执行的,而加锁,只是加锁的部分即修改共享数据的部分是串行的
#单从保证数据安全方面,二者都可以实现,但很明显是加锁的效率更高.
from threading import current_thread,Thread,Lock
import os,time
def task():
time.sleep(3)
print('%s start to run' %current_thread().getName())
global n
temp=n
time.sleep(0.5)
n=temp-1
if __name__ == '__main__':
n=100
lock=Lock()
start_time=time.time()
for i in range(100):
t=Thread(target=task)
t.start()
t.join()
stop_time=time.time()
print('主:%s n:%s' %(stop_time-start_time,n))
'''
100 Thread-1 start to run
101 Thread-2 start to run
102 ......
103 Thread-100 start to run
104 主:350.6937336921692 n:0 #耗时是多么的恐怖
105 '''
![]()
![]()
from threading import Thread,Lock
import time
mutex=Lock()
x=100
def task():
global x
mutex.acquire()
temp=x
time.sleep(0.1)
x=temp-1
mutex.release()
if __name__ == '__main__':
start=time.time()
t_1=[]
for i in range(100):
t=Thread(target=task,)
t_1.append(t)
t.start()
for t in t_1:
t.join()
print('主',x)
print(time.time()-start)
![]()
from threading import Thread,Lock,RLock
import time
# mutexA=Lock()
# mutexB=Lock()
mutexA=mutexB=RLock()
class MyThread(Thread):
def run(self):
self.f1()
self.f2()
def f1(self):
mutexA.acquire()
print('%s 拿到了A锁' %self.name)
mutexB.acquire()
print('%s 拿到了B锁' %self.name)
mutexB.release()
mutexA.release()
def f2(self):
mutexB.acquire()
print('%s 拿到了B锁' %self.name)
time.sleep(0.1)
mutexA.acquire()
print('%s 拿到了A锁' %self.name)
mutexA.release()
mutexB.release()
if __name__ == '__main__':
for i in range(10):
t=MyThread()
t.start()
# t1=MyThread()
# t1.start()
#
# t2=MyThread()
# t2.start()
#
# t3=MyThread()
# t3.start()
print('主')
![]()
![]()
![]()