标签:after reading order shu comment san 自己的 lin test
Python多线程和多进程
"""
多线程实现方式一:
创建 Thread 的实例,传给它一个函数。
时间:2019年11月24日
author:戴昊龙
"""
from threading import Thread
from time import sleep, ctime
loops = [4, 2]
def loop(nloop, nsec):
print(‘start loop‘, nloop, ‘at:‘, ctime())
sleep(nsec)
print(‘loop‘, nloop, ‘done at‘, ctime())
def main():
print("starting at:", ctime())
threads = [] # 设置线程数
nloops = range(len(loops))
for i in nloops:
t = Thread(target=loop, args=(i, loops[i])) # target传入的是待执行多线程的函数名,args传入的是这个函数需要的参数
threads.append(t)
for i in nloops:
threads[i].start() # 开始执行这个线程
for i in nloops:
threads[i].join() # join() 方法,可以让主线程等待所有线程执行完毕。
print("all done at", ctime())
if __name__ == ‘__main__‘:
main()
"""
子类化的Thread,来实现多线程
时间:2019年11月24日
author:戴昊龙
"""
from threading import Thread
from time import ctime, sleep
loops = [4, 2]
class MyThread(Thread):
def __init__(self, func, agrs, name=‘‘):
super().__init__() # 等价于super(MyThread, self).__init__()
# 上面这个super().__init__() 为什么没有填父类的参数? 不是说要完全继承父类的属性吗?怎么子类非但没有继承,还自己加了三个属性?
# 这个是因为Thread类,中init方法,含有默认参数,父类的属性均已传默认参数
"""
阅读源码可知def __init__(self, group=None, target=None, name=None,
args=(), kwargs=None, *, daemon=None):
因此子类继承的时候,只需要简写就可以了,会默认全部继承,子类需要新加属性,也只需要自己在init方法中新增就可以
但是还是得调用父类的init方法,只是可以简单的省略其中的参数
"""
self.func = func
self.args = agrs
self.name = name
def run(self):
self.func(*self.args)
def loop(nloop, nsec):
print("start loop", nloop, ‘at:‘, ctime())
sleep(nsec)
print(‘loop‘, nloop, "done at", ctime())
def main():
print(‘starting at:‘, ctime())
threads = []
nloops = range(len(loops))
for i in nloops:
t = MyThread(loop, (i, loops[i]), loop.__name__)
threads.append(t)
for i in nloops:
threads[i].start()
for i in nloops:
threads[i].join()
print("all done at:", ctime())
if __name__ == ‘__main__‘:
main()
# 不加锁的情况下
from threading import Thread
from random import randint
from time import ctime, sleep
list1 = [‘dai‘, ‘hao‘, ‘long‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘shuai‘, ‘bi‘]
def modify_list():
print("现在时间是%s, 当前的list是:" % ctime(), list1 )
sleep(1)
list1.insert(0, randint(0,10))
sleep(1)
print("执行insert之后现在时间是%s, 当前的list是:" % ctime(), list1)
sleep(1)
list1.pop(randint(0, len(list1)))
sleep(1)
print("执行pop之后现在时间是%s, 当前的list是:" % ctime(), list1)
return list1
for x in range(5):
Thread(target=modify_list, args=()).start()
# modify_list()
"""
I:\python_auto_test\pytest_task\venv\Scripts\python.exe I:/python_auto_test/pytest_task/thread_demo.py
现在时间是Sun Dec 1 16:52:42 2019, 当前的list是: [‘dai‘, ‘hao‘, ‘long‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘shuai‘, ‘bi‘]
Thread-1
执行insert之后现在时间是Sun Dec 1 16:52:42 2019, 当前的list是: [5, ‘dai‘, ‘hao‘, ‘long‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘shuai‘, ‘bi‘]
现在时间是Sun Dec 1 16:52:42 2019, 当前的list是: [5, ‘dai‘, ‘hao‘, ‘long‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘shuai‘, ‘bi‘]
执行pop之后现在时间是Sun Dec 1 16:52:42 2019, 当前的list是: [5, ‘dai‘, ‘hao‘, ‘long‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘bi‘]
Thread-2
执行insert之后现在时间是Sun Dec 1 16:52:42 2019, 当前的list是: [4, 5, ‘dai‘, ‘hao‘, ‘long‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘bi‘]
执行pop之后现在时间是Sun Dec 1 16:52:42 2019, 当前的list是: [4, 5, ‘hao‘, ‘long‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘bi‘]
现在时间是Sun Dec 1 16:52:42 2019, 当前的list是: [4, 5, ‘hao‘, ‘long‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘bi‘]
Thread-3
现在时间是Sun Dec 1 16:52:42 2019, 当前的list是: [4, 5, ‘hao‘, ‘long‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘bi‘]
执行insert之后现在时间是Sun Dec 1 16:52:42 2019, 当前的list是: [1, 4, 5, ‘hao‘, ‘long‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘bi‘]
执行pop之后现在时间是Sun Dec 1 16:52:42 2019, 当前的list是: [1, 4, 5, ‘hao‘, ‘long‘, ‘wo‘, ‘shi‘, ‘ge‘]
Thread-4
执行insert之后现在时间是Sun Dec 1 16:52:42 2019, 当前的list是: [2, 1, 4, 5, ‘hao‘, ‘long‘, ‘wo‘, ‘shi‘, ‘ge‘]
现在时间是Sun Dec 1 16:52:42 2019, 当前的list是: [2, 1, 4, 5, ‘hao‘, ‘long‘, ‘wo‘, ‘shi‘, ‘ge‘]
执行pop之后现在时间是Sun Dec 1 16:52:42 2019, 当前的list是: [2, 1, 4, 5, ‘long‘, ‘wo‘, ‘shi‘, ‘ge‘]
Thread-5
执行insert之后现在时间是Sun Dec 1 16:52:42 2019, 当前的list是: [0, 2, 1, 4, 5, ‘long‘, ‘wo‘, ‘shi‘, ‘ge‘]
执行pop之后现在时间是Sun Dec 1 16:52:42 2019, 当前的list是: [0, 2, 1, 4, 5, ‘long‘, ‘shi‘, ‘ge‘]
Process finished with exit code 0
"""
# 加锁的情况下
from threading import Thread, Lock
from random import randint
from time import ctime, sleep
list1 = [‘dai‘, ‘hao‘, ‘long‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘shuai‘, ‘bi‘]
lock = Lock()
def modify_list():
print("现在时间是%s, 当前的list是:" % ctime(), list1 )
lock.acquire()
list1.insert(0, randint(0,10))
print("执行insert之后现在时间是%s, 当前的list是:" % ctime(), list1)
list1.pop(randint(0, len(list1)))
lock.release()
print("执行pop之后现在时间是%s, 当前的list是:" % ctime(), list1)
return list1
for x in range(5):
Thread(target=modify_list, args=()).start()
# modify_list()
"""
现在时间是Sun Dec 1 16:52:04 2019, 当前的list是: [‘dai‘, ‘hao‘, ‘long‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘shuai‘, ‘bi‘]
Thread-1
执行insert之后现在时间是Sun Dec 1 16:52:04 2019, 当前的list是: [4, ‘dai‘, ‘hao‘, ‘long‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘shuai‘, ‘bi‘]
现在时间是Sun Dec 1 16:52:04 2019, 当前的list是: [4, ‘dai‘, ‘hao‘, ‘long‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘shuai‘, ‘bi‘]
执行pop之后现在时间是Sun Dec 1 16:52:04 2019, 当前的list是: [4, ‘dai‘, ‘hao‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘shuai‘, ‘bi‘]
Thread-2
执行insert之后现在时间是Sun Dec 1 16:52:04 2019, 当前的list是: [7, 4, ‘dai‘, ‘hao‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘shuai‘, ‘bi‘]
执行pop之后现在时间是Sun Dec 1 16:52:04 2019, 当前的list是: [7, 4, ‘dai‘, ‘hao‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘bi‘]
现在时间是Sun Dec 1 16:52:04 2019, 当前的list是: [7, 4, ‘dai‘, ‘hao‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘bi‘]
Thread-3
执行insert之后现在时间是Sun Dec 1 16:52:04 2019, 当前的list是: [2, 7, 4, ‘dai‘, ‘hao‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘bi‘]
执行pop之后现在时间是Sun Dec 1 16:52:04 2019, 当前的list是: [2, 7, 4, ‘dai‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘bi‘]
现在时间是Sun Dec 1 16:52:04 2019, 当前的list是: [2, 7, 4, ‘dai‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘bi‘]
Thread-4
现在时间是Sun Dec 1 16:52:04 2019, 当前的list是: [2, 7, 4, ‘dai‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘bi‘]
执行insert之后现在时间是Sun Dec 1 16:52:04 2019, 当前的list是: [10, 2, 7, 4, ‘dai‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘bi‘]
执行pop之后现在时间是Sun Dec 1 16:52:04 2019, 当前的list是: [2, 7, 4, ‘dai‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘bi‘]
Thread-5
执行insert之后现在时间是Sun Dec 1 16:52:04 2019, 当前的list是: [8, 2, 7, 4, ‘dai‘, ‘wo‘, ‘shi‘, ‘ge‘, ‘bi‘]
执行pop之后现在时间是Sun Dec 1 16:52:04 2019, 当前的list是: [8, 2, 7, 4, ‘wo‘, ‘shi‘, ‘ge‘, ‘bi‘]
"""
concurrent.futures
模块提供异步执行回调高层接口。标签:after reading order shu comment san 自己的 lin test
原文地址:https://www.cnblogs.com/dadaizi/p/13060427.html