python多线程与多进程
化形还飞飞
这个作者很懒,什么都没留下…
展开
-
Python之多进程编程7,multiprocessing的使用
Python之多进程编程7,multiprocessing的使用1、multiprocess.Process该方法为创建一个进程,在创建多个进程时系统默认进行并发操作导入库import multiprocessing创建Process对象以及进程的启动process=multiprocessing.Process(target=function,args=*)process.start()创建函数functiondef onefunction(n): print("原创 2020-11-05 20:08:37 · 215 阅读 · 0 评论 -
Python之多线程编程6,线程池(ThreadPoolExecutor)
Python之多线程编程6,线程池(ThreadPoolExecutor)线程池是专门用来处理多线程的,当有多任务时,将任务提交至线程池,线程池可内部解决线程问题。当线程数达到设定的最大值时,线程池内部会进行阻塞,等到其他线程完成后可再增加线程线程池对象主要有三个方法submit():将任务提交至线程池,线程池内部进行调度result():返回某任务的返回值done():判断某任务是否完成函数库的导入#导入函数库import concurrent.futures import Thread原创 2020-11-04 19:00:30 · 243 阅读 · 0 评论 -
Python之多线程编程5,线程同步之Semaphore(线程数量控制)
Python之多线程编程5,线程同步之Semaphore(线程数量控制)函数库的导入from threadingimport time函数的调用#arg默认参数为可同时运行的线程数sem=threading.Semaphore(arg)方法函数的建立#调用本函来循环20次,每次多条线程进行,线程数为Semaphore的参数class twoclass(threading.Thread): def __init__(self, sem): super().__in原创 2020-11-04 16:27:48 · 241 阅读 · 0 评论 -
Python之多线程编程4,线程同步之Condition
Python之多线程编程4,线程同步之Condition该例子主要为小明与小红的对话,通过两个线程实现交流Condition的导入from threading import Condition创建小明与小红两个类#小红类的创建class XiaoHong(threading.Thread): #构造函数 def __init__(self, con): super().__init__() self.condition=con原创 2020-11-02 16:54:04 · 242 阅读 · 0 评论 -
Python之多线程编程3,线程同步之Lock
线程同步Lock(线程锁)导入Lock方法from threading import Lock lock=Lock()Lock的使用lock.acquire() 线程的获取lock.arquire() 线程的释放实例:全局变量及库的导入import threadingfrom threading import Locklock = threading.Lock()total = 0函数的编写def function_add(): global t原创 2020-11-02 15:39:17 · 1031 阅读 · 0 评论 -
Python之多线程编程2,线程间的通信(全局变量与Queue)
Python之多线程编程2,线程间的通信1、通过全局变量线程间通信创建全局变量value=[]创建函数调用全局变量#向列表队尾追加元素def function_append(n, threadname): global value print(f"运行线程为:{threadname},value的值为{value}") for i in range(n): value.append(i) print(f"添加后value的值为{v原创 2020-11-01 21:17:27 · 533 阅读 · 0 评论 -
Python之多线程编程1,线程的创建与实现
Python之多线程编程1,线程的创建与实现1、线程类对象的创建#导入线程库threadingimport threading#线程的创建thread1 = threading.Thread(target=function,args=[2,"thread1"])thread2 = threading.Thread(target=function,args=[3,"thread2"])#导入时间库time#import time#定义线程运行的函数def function(n,threa原创 2020-11-01 16:57:39 · 847 阅读 · 0 评论