并行编程概述
Posted shirleysu90
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了并行编程概述相关的知识,希望对你有一定的参考价值。
并发编程
多线程之——threa模块
>>> import time >>> time.ctime() ‘Thu Jan 9 07:52:57 2020‘
import time """单线程示例 """ def worker(n): print(f‘函数执行开始于:{time.ctime()}‘) time.sleep(n)#休眠时间 print(f‘函数执行结束于:{time.ctime()}‘) def mian(): print(f‘【主函数执行开始于:{time.ctime()}】‘)#f格式化字符串 worker(4)#执行4秒 worker(2)#执行2秒 print(f‘【主函数执行结束于:{time.ctime()}】‘) if __name__ == ‘__main__‘: mian()
结果
【主函数执行开始于:Thu Jan 9 08:00:57 2020】 函数执行开始于:Thu Jan 9 08:00:57 2020 函数执行结束于:Thu Jan 9 08:01:01 2020 函数执行开始于:Thu Jan 9 08:01:01 2020 函数执行结束于:Thu Jan 9 08:01:03 2020 【主函数执行结束于:Thu Jan 9 08:01:03 2020】
多线程
_thread模式弊端并不能决定函数 程序什么时候结束
import time import _thread """_thread 实现多线程 """ def worker(n): print(f‘函数执行开始于:{time.ctime()}‘) time.sleep(n)#休眠时间 print(f‘函数执行结束于:{time.ctime()}‘) def mian():#多线程只修改main函数 print(f‘【主函数执行开始于:{time.ctime()}】‘)#f格式化字符串 _thread.start_new_thread(worker,(4,))#开始一个新的线程,function函数名称叫worker,args传递参数,以元组形式展现 _thread.start_new_thread(worker, (2,)) print(f‘【主函数执行结束于:{time.ctime()}】‘) if __name__ == ‘__main__‘: mian()
结果
【主函数执行开始于:Thu Jan 9 08:14:44 2020】 【主函数执行结束于:Thu Jan 9 08:14:44 2020】
想执行等一等 就是加上time sleep
import time import _thread """_thread 实现多线程 """ def worker(n): print(f‘函数执行开始于:{time.ctime()}‘) time.sleep(n)#休眠时间 print(f‘函数执行结束于:{time.ctime()}‘) def mian():#多线程只修改main函数 print(f‘【主函数执行开始于:{time.ctime()}】‘)#f格式化字符串 _thread.start_new_thread(worker,(4,))#开始一个新的线程,function函数名称叫worker,args传递参数,以元组形式展现 _thread.start_new_thread(worker, (2,)) time.sleep(6) print(f‘【主函数执行结束于:{time.ctime()}】‘) if __name__ == ‘__main__‘: mian()
threading 模块
主函数的主线程没有和子线程同步
import time import threading """threading.Thread() 实现多线程 """ def worker(n): print(f‘函数执行开始于:{time.ctime()}‘) time.sleep(n)#休眠时间 print(f‘函数执行结束于:{time.ctime()}‘) def mian():#多线程只修改main函数 print(f‘【主函数执行开始于:{time.ctime()}】‘)#f格式化字符串 #_thread.start_new_thread(worker,(4,))#开始一个新的线程,function函数名称叫worker,args传递参数,以元组形式展现 #_thread.start_new_thread(worker, (2,)) threads = []#先申请一个数组让他为空 t1 = threading.Thread(target=worker, args=(4,))#构造一个他t1线程,执行目标函数,atgs耗费4秒 threads.append(t1) #追加t1线程 t2 = threading.Thread(target=worker, args=(2,)) threads.append(t2) for t in threads: t.start() #time.sleep(6) print(f‘【主函数执行结束于:{time.ctime()}】‘) if __name__ == ‘__main__‘: mian()
理想状态是等待当前状态执行完毕之后再主线程执行完
import time import threading """threading.Thread() 实现多线程 """ def worker(n): print(f‘函数执行开始于:{time.ctime()}‘) time.sleep(n)#休眠时间 print(f‘函数执行结束于:{time.ctime()}‘) def mian():#多线程只修改main函数 print(f‘【主函数执行开始于:{time.ctime()}】‘)#f格式化字符串 #_thread.start_new_thread(worker,(4,))#开始一个新的线程,function函数名称叫worker,args传递参数,以元组形式展现 #_thread.start_new_thread(worker, (2,)) threads = []#先申请一个数组让他为空 t1 = threading.Thread(target=worker, args=(4,))#构造一个他t1线程,执行目标函数,atgs耗费4秒 threads.append(t1) #追加t1线程 t2 = threading.Thread(target=worker, args=(2,)) threads.append(t2) for t in threads: t.start() for t in threads: t.join() #time.sleep(6) print(f‘【主函数执行结束于:{time.ctime()}】‘) if __name__ == ‘__main__‘: mian()
import time import threading """threading.Thread() 实现多线程 """ def worker(n): print(‘{} 函数执行开始于:{}‘.format(threading.current_thread().name, time.ctime()))#出现当前线程名称 time.sleep(n)#休眠时间 print(f‘{threading.current_thread().name} 函数执行结束于:{time.ctime()}‘)#获得当前线程 def mian():#多线程只修改main函数 print(f‘【主函数执行开始于:{time.ctime()}】‘)#f格式化字符串 #_thread.start_new_thread(worker,(4,))#开始一个新的线程,function函数名称叫worker,args传递参数,以元组形式展现 #_thread.start_new_thread(worker, (2,)) threads = []#先申请一个数组让他为空 t1 = threading.Thread(target=worker, args=(4,))#构造一个他t1线程,执行目标函数,atgs耗费4秒 threads.append(t1) #追加t1线程 t2 = threading.Thread(target=worker, args=(2,)) threads.append(t2) for t in threads: t.start() for t in threads: t.join() #time.sleep(6) print(f‘【主函数执行结束于:{time.ctime()}】‘) if __name__ == ‘__main__‘: mian()
结果
【主函数执行开始于:Thu Jan 9 10:33:17 2020】 Thread-1 函数执行开始于:Thu Jan 9 10:33:17 2020 Thread-2 函数执行开始于:Thu Jan 9 10:33:17 2020 Thread-2 函数执行结束于:Thu Jan 9 10:33:19 2020 Thread-1 函数执行结束于:Thu Jan 9 10:33:21 2020 【主函数执行结束于:Thu Jan 9 10:33:21 2020】
派生类
import time import threading """threading.Thread() 实现多线程 """ def worker(n): print(‘{} 函数执行开始于:{}‘.format(threading.current_thread().name, time.ctime()))#出现当前线程名称 time.sleep(n)#休眠时间 print(f‘{threading.current_thread().name} 函数执行结束于:{time.ctime()}‘)#获得当前线程 class MyThread(threading.Thread):#继承基类 def __init__(self, func, args):#指定信息 threading.Thread.__init__(self) self.func = func self.args = args def run(self): self.func(*self.args) def mian():#多线程只修改main函数 print(f‘【主函数执行开始于:{time.ctime()}】‘)#f格式化字符串 #_thread.start_new_thread(worker,(4,))#开始一个新的线程,function函数名称叫worker,args传递参数,以元组形式展现 #_thread.start_new_thread(worker, (2,)) threads = []#先申请一个数组让他为空 t1 = MyThread(worker, (4,)) threads.append(t1) #追加t1线程 #t2 = threading.Thread(target=worker, args=(2,)) t2 = MyThread(worker,(2,)) threads.append(t2) for t in threads: t.start() for t in threads: t.join() #time.sleep(6) print(f‘【主函数执行结束于:{time.ctime()}】‘) if __name__ == ‘__main__‘: mian()
结果
【主函数执行开始于:Thu Jan 9 10:44:08 2020】 Thread-1 函数执行开始于:Thu Jan 9 10:44:08 2020 Thread-2 函数执行开始于:Thu Jan 9 10:44:08 2020 Thread-2 函数执行结束于:Thu Jan 9 10:44:10 2020 Thread-1 函数执行结束于:Thu Jan 9 10:44:12 2020 【主函数执行结束于:Thu Jan 9 10:44:12 2020】
同步锁
import threading import time """同步原语之:锁 """ eggs = [] def put_egg(n, lst): for i in range(1,n+1):#做遍历 lst.append(i) def main(): threads = [] #变量等于数组 for i in range(3): t = threading.Thread(target=put_egg,args=(5,eggs)) threads.append(t) for t in threads: t.start() for t in threads: t.join() print(eggs) if __name__ == ‘__main__‘: main()
结论
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
import threading import time import random """同步原语之:锁 """ eggs = [] lock = threading.Lock()#声明变量 def put_egg(n, lst): lock.acquire() for i in range(1,n+1):#做遍历 time.sleep(random.randint(0, 2)) lst.append(i) lock.release() def main(): threads = [] #变量等于数组 for i in range(3): t = threading.Thread(target=put_egg,args=(5,eggs)) threads.append(t) for t in threads: t.start() for t in threads: t.join() print(eggs) if __name__ == ‘__main__‘: main()
qunue队列
由生产者每个半秒向队列里放入一个随机数,总共放5个,先进先出,在队列里有消费者线程来取到某一项进行处理,
处理完毕后一个task——down告诉任务结束,取内容有个超时操作3秒,取不到就不取得到一个空的异常
import threading #输入模块 import queue#队列模块 import time#时间模块 import random#使用随机数 """LIFO 队列示例 """ def producer(data_queue):#定义生产者函数 for i in range(5):#循环5次 time.sleep(0.5)#每循环一次停留一会儿 item = random.randint(1, 100)#1-100间随便一个数字 data_queue.put(item)#队列里放入一个信息,把item放入进去 print(f‘{threading.current_thread().name} 在队列中放入数据项:{item}‘) #放入对象到队列的方法 def consumer(data_queue): while True: try: item = data_queue.get(timeout=3)#获取信息,3秒钟得不到算超时 print(f‘{threading.current_thread().name} 从队列中移除了 {item}‘)#当前线程从队列中移除 except queue.Empty:#捕获异常 break#跳出当前循环 else:#声明当前数据项处理完毕 data_queue.task_done() def main():#定义主函数,声明对象 q = queue.Queue() threads = [] p = threading.Thread(target=producer, args=(q,)) p.start() for i in range(2):#创建两个消费者线程 c = threading.Thread(target=consumer, args=(q,)) threads.append(c) for t in threads: t.start() for t in threads: t.join()#让主线程等待当前线程执行完毕 q.join()#让主线程等待整个队列的操作 if __name__ == ‘__main__‘: main()
import multiprocessing import time """多进程示例 """ def func(n): print(f‘{multiprocessing.current_process().name} 执行开始于:{time.ctime()}‘)#当前进程有个属性叫name time.sleep(n) print(f‘{multiprocessing.current_process().name} 执行结束于:{time.ctime()}‘) def main(): print(f‘主函数执行于:{time.ctime()}‘) processes = [] p1 = multiprocessing.Process(target=func, args=(4,)) processes.append(p1) p2 = multiprocessing.Process(target=func, args=(2,)) processes.append(p2) print(f‘主函数结束于:{time.ctime()}‘) if __name__ == ‘__main__‘: main() print(f‘主函数结束于:{time.ctime()}‘)
更多并行编程
import time import concurrent.futures """concurrent.futures应用 """ numbers = list(range(1,11))#做一个列表,生成范围1-10 def count(n):#定义函数,接收参数n for i in range(1000000): i += i return i * n def worker(x): result = count(x) #计算 print(f‘数字:{x}的计算结果是:{result}‘) #顺序执行 def sequential_execution(): start_time = time.clock() for i in numbers: worker(i) print(f‘顺序执行花费时间:{time.clock()-start_time}秒‘) #线程池执行 def threading_execution(): start_time = time.clock() with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: for i in numbers: executor.submit(worker, i)#分配调度使用函数 print(f‘线程池执行花费时间:{time.clock()-start_time}秒‘) #进程池执行 def process_execution(): start_time = time.clock() with concurrent.futures.ProcessPoolExecutor(max_workers=5) as executor: for i in numbers: executor.submit(worker, i) print(f‘进程池执行花费时间:{time.clock()-start_time}秒‘) if __name__ == ‘__main__‘: #sequential_execution() #threading_execution()
process_execution()
函数基础
将函数赋给变量
In[2]: def hello(name): ...: print(‘Hello ‘, name) ...: In[3]: hello(‘Tom‘)#函数名称加特定参数 Hello Tom In[4]: type(hello)#用type检查 Out[4]: function In[5]: hello Out[5]: <function __main__.hello(name)> In[6]: greeting = hello In[7]: greeting Out[7]: <function __main__.hello(name)> In[8]: hello(‘Tom‘) Hello Tom In[9]: greeting(‘Jerry‘) Hello Jerry
将函数作为参数传递
In[2]: def add(a, b):#把两个参数相加 ...: return a + b ...: In[4]: def subtract(a, b): ...: return a - b ...: In[5]: add(3,5) Out[5]: 8 In[6]: subtract(5,3) Out[6]: 2 In[7]: def action(x, y, func): ...: return func(x, y) ...: In[8]: action(5, 3, add) Out[8]: 8 In[9]: action(5, 3, subtract) Out[9]: 2 In[10]: action(5, 3, lambda x, y:x*y) Out[10]: 15
函数嵌套及跨域访问
加括号是返回执行结果,不加括号是返回当前的对象
>>> def greeting():#定义函数greeting
... def hello():#函数里定义一个函数greeting
... return ‘Hello‘
... return hello()#hello作为一个字符串在greeting结果返回
...
>>> greeting()#调用
‘Hello‘
>>> def greeting():
... def hello():
... return ‘Hello‘
... return hello
...
>>> greeting()
<function greeting.<locals>.hello at 0x00000000037E31E0>
>>> greeting()()#想执行再写个括号
‘Hello‘
函数嵌套域成员访问
>>> def func_1(): ... x = 10 ... def func_2(): ... x = 20 ... return x + 10 ... return func_2() ... >>> func_1()#用的是本地变量与外侧 30 想用func1里func2外面的 >> def func_1(): ... x = 10 ... def func_2(): ... nonlocal x #非本地的x,使用上一层函数封装区域 ... return x + 10 ... return func_2() ... >>> func_1() 20
函数定义装饰器
"""函数定义装饰器 """ def p_decoratior(func):#把gettext传入装饰器里 def wrapper(*args,**kwargs):#args元组,kwargs有名字的参数,任何类型参数都可通过它来表示 return ‘<p>‘ + func(*args,**kwargs) + ‘</p>‘ return wrapper @p_decoratior#对gettext做个装饰器,进行装饰 def get_text(): return ‘欢迎学习优品课堂课程‘ if __name__ == ‘__main__‘: print(get_text())
同
"""函数定义装饰器 """ def p_decoratior(func):#把gettext传入装饰器里 def wrapper(*args,**kwargs):#args元组,kwargs有名字的参数,任何类型参数都可通过它来表示 return ‘<p>‘ + func(*args,**kwargs) + ‘</p>‘ return wrapper #@p_decoratior#对gettext做个装饰器,进行装饰 def get_text(): return ‘欢迎学习优品课堂课程‘ if __name__ == ‘__main__‘: html = p_decoratior(get_text)#把gettext函数作为参数传递到decoratorfunc里 print(html())#结果返回的字符串再打印
"""函数定义装饰器 """ def p_decoratior(func):#把gettext传入装饰器里 def wrapper(*args,**kwargs):#args元组,kwargs有名字的参数,任何类型参数都可通过它来表示 return ‘<p>‘ + func(*args,**kwargs) + ‘</p>‘ return wrapper @p_decoratior#对gettext做个装饰器,进行装饰 def get_text(): return ‘欢迎学习优品课堂课程‘ def get_upper_text(text): return text.upper() if __name__ == ‘__main__‘: print(get_upper_text(‘www.codeclassroom.com‘))
装饰器修饰
"""函数定义装饰器 """ def p_decoratior(func):#把gettext传入装饰器里 def wrapper(*args,**kwargs):#args元组,kwargs有名字的参数,任何类型参数都可通过它来表示 return ‘<p>‘ + func(*args,**kwargs) + ‘</p>‘ return wrapper @p_decoratior#对gettext做个装饰器,进行装饰 def get_text(): return ‘欢迎学习优品课堂课程‘ @p_decoratior def get_upper_text(text): return text.upper() if __name__ == ‘__main__‘: print(get_upper_text(‘www.codeclassroom.com‘))
类定义装饰器
"""类定义装饰器 """ class P: def __init__(self, func): #构造传递函数 self.func = func def __call__(self, *args, **kwargs):#调用 return ‘<p>‘ + self.func(*args,**kwargs) + ‘</p>‘#返回p标签加/p标签 def get_text(): return ‘欢迎学习优品课堂课程‘ def get_upper_text(text): return text.upper() if __name__ == ‘__main__‘: print(get_text())#未加修饰的
想修饰
"""类定义装饰器 """ class P: def __init__(self, func): #构造传递函数 self.func = func def __call__(self, *args, **kwargs):#调用 return ‘<p>‘ + self.func(*args,**kwargs) + ‘</p>‘#返回p标签加/p标签 @P def get_text(): return ‘欢迎学习优品课堂课程‘ def get_upper_text(text): return text.upper() if __name__ == ‘__main__‘: print(get_text())#未加修饰的
函数定义装饰器
"""函数定义装饰器 """ def p_decoratior(func):#把gettext传入装饰器里 def wrapper(*args,**kwargs):#args元组,kwargs有名字的参数,任何类型参数都可通过它来表示 return ‘<p>‘ + func(*args,**kwargs) + ‘</p>‘ return wrapper class Student: def __init__(self, name): self.name = name @p_decoratior def get_name(self): return self.name.upper() @p_decoratior#对gettext做个装饰器,进行装饰 def get_text(): return ‘欢迎学习优品课堂课程‘ @p_decoratior def get_upper_text(): return text.upper() if __name__ == ‘__main__‘: s = Student(‘Mike‘) print(s.get_name())
类定义装饰器
"""类定义装饰器 """ class P: def __init__(self, func): #构造传递函数 self.func = func def __call__(self, *args, **kwargs):#调用 return ‘<p>‘ + self.func(*args,**kwargs) + ‘</p>‘#返回p标签加/p标签 class Student: def __init__(self,name): self.name = name def get_name(self): return self.name.upper() @P def get_text(): return ‘欢迎学习优品课堂课程‘ @P def get_upper_text(text): return text.upper() if __name__ == ‘__main__‘: s = Student(‘John‘) print(s.get_name())
参数化装饰器
"""函数定义装饰器 """ def p_decorator(func):#把gettext传入装饰器里 def wrapper(*args,**kwargs):#args元组,kwargs有名字的参数,任何类型参数都可通过它来表示 return ‘<p>‘ + func(*args,**kwargs) + ‘</p>‘ return wrapper def div_decorator(func): def wrapper(*args,**kwargs): return ‘<div>{}</div>‘.format(func(*args, **kwargs)) return wrapper class Student:... @p_decorator#对gettext做个装饰器,进行装饰 def get_text(): return ‘欢迎学习优品课堂课程‘ @div_decorator def get_upper_text(text): return text.upper() if __name__ == ‘__main__‘: print(get_upper_text(‘www.codeclassroom.com‘))
再加个@p_decorator,看看修饰的先后
"""函数定义装饰器 """ def p_decorator(func):#把gettext传入装饰器里 def wrapper(*args,**kwargs):#args元组,kwargs有名字的参数,任何类型参数都可通过它来表示 return ‘<p>‘ + func(*args,**kwargs) + ‘</p>‘ return wrapper def div_decorator(func): def wrapper(*args,**kwargs): return ‘<div>{}</div>‘.format(func(*args, **kwargs)) return wrapper class Student:... @p_decorator#对gettext做个装饰器,进行装饰 def get_text(): return ‘欢迎学习优品课堂课程‘ @div_decorator @p_decorator def get_upper_text(text): return text.upper() if __name__ == ‘__main__‘: print(get_upper_text(‘www.codeclassroom.com‘))
不加装饰器 原本返回一个地址
"""函数定义装饰器 """ #参数化装饰器 def tags(tag): def tag_decorator(func): def wrapper(*args,**kwargs): return f‘<{tag}>{func(*args,**kwargs)}</{tag}>‘#返回格式化字符串 return wrapper return tag_decorator def p_decorator(func):#把gettext传入装饰器里 def wrapper(*args,**kwargs):#args元组,kwargs有名字的参数,任何类型参数都可通过它来表示 return ‘<p>‘ + func(*args,**kwargs) + ‘</p>‘ return wrapper def div_decorator(func): def wrapper(*args,**kwargs): return ‘<div>{}</div>‘.format(func(*args, **kwargs)) return wrapper class Student:... #@p_decorator#对gettext做个装饰器,进行装饰 def get_text(): return ‘欢迎学习优品课堂课程‘ #@div_decorator #@p_decorator() def get_upper_text(text): return text.upper() if __name__ == ‘__main__‘: print(get_upper_text(‘www.codeclassroom.com‘))
加一个标签,装饰器叫tags
"""函数定义装饰器 """ #参数化装饰器 def tags(tag): def tag_decorator(func): def wrapper(*args,**kwargs): return f‘<{tag}>{func(*args,**kwargs)}</{tag}>‘#返回格式化字符串 return wrapper return tag_decorator def p_decorator(func):#把gettext传入装饰器里 def wrapper(*args,**kwargs):#args元组,kwargs有名字的参数,任何类型参数都可通过它来表示 return ‘<p>‘ + func(*args,**kwargs) + ‘</p>‘ return wrapper def div_decorator(func): def wrapper(*args,**kwargs): return ‘<div>{}</div>‘.format(func(*args, **kwargs)) return wrapper class Student:... #@p_decorator#对gettext做个装饰器,进行装饰 def get_text(): return ‘欢迎学习优品课堂课程‘ #@div_decorator #@p_decorator() @tags(‘p‘)#加标签 def get_upper_text(text): return text.upper() if __name__ == ‘__main__‘: print(get_upper_text(‘www.codeclassroom.com‘))
想变div
"""函数定义装饰器 """ #参数化装饰器 def tags(tag): def tag_decorator(func): def wrapper(*args,**kwargs): return f‘<{tag}>{func(*args,**kwargs)}</{tag}>‘#返回格式化字符串 return wrapper return tag_decorator def p_decorator(func):#把gettext传入装饰器里 def wrapper(*args,**kwargs):#args元组,kwargs有名字的参数,任何类型参数都可通过它来表示 return ‘<p>‘ + func(*args,**kwargs) + ‘</p>‘ return wrapper def div_decorator(func): def wrapper(*args,**kwargs): return ‘<div>{}</div>‘.format(func(*args, **kwargs)) return wrapper class Student:... #@p_decorator#对gettext做个装饰器,进行装饰 def get_text(): return ‘欢迎学习优品课堂课程‘ #@div_decorator #@p_decorator() @tags(‘div‘)#加标签 def get_upper_text(text): return text.upper() if __name__ == ‘__main__‘: print(get_upper_text(‘www.codeclassroom.com‘))
"""函数定义装饰器 """ #参数化装饰器 def tags(tag): def tag_decorator(func): def wrapper(*args,**kwargs): return f‘<{tag}>{func(*args,**kwargs)}</{tag}>‘#返回格式化字符串 return wrapper return tag_decorator def p_decorator(func):#把gettext传入装饰器里 def wrapper(*args,**kwargs):#args元组,kwargs有名字的参数,任何类型参数都可通过它来表示 return ‘<p>‘ + func(*args,**kwargs) + ‘</p>‘ return wrapper def div_decorator(func): def wrapper(*args,**kwargs): return ‘<div>{}</div>‘.format(func(*args, **kwargs)) return wrapper class Student:... #@p_decorator#对gettext做个装饰器,进行装饰 def get_text(): return ‘欢迎学习优品课堂课程‘ #@div_decorator #@p_decorator() @tags(‘div‘)#加标签 @tags(‘p‘) def get_upper_text(text): return text.upper() if __name__ == ‘__main__‘: print(get_upper_text(‘www.codeclassroom.com‘))
python sublime text python 配置
以上是关于并行编程概述的主要内容,如果未能解决你的问题,请参考以下文章
[工作积累] UE4 并行渲染的同步 - Sync between FParallelCommandListSet & FRHICommandListImmediate calls(代码片段