线程
import threading
锁
lock=threading.Lock() #
lock=threading.RLock() #递归锁,多层锁定,多层解锁
lock.acquire()
lock.release()
import threading import time v = 10 #lock = threading.Lock() # 只能开一把 lock = threading.RLock()# 可以开多把 def task(arg): time.sleep(2) # 申请使用锁,其他人等 lock.acquire() lock.acquire() global v v -= 1 print(v) # 释放 lock.release() lock.release() for i in range(10): t = threading.Thread(target=task,args=(i,)) t.start()
多个人使用锁
lock = threading.BoundedSemaphore(3)
lock.acquire()
lock.release()
import threading import time lock = threading.BoundedSemaphore(3) def task(arg): # 申请使用锁,其他人等 lock.acquire() time.sleep(1) global v v -= 1 print(v) # 释放 lock.release() for i in range(10): t = threading.Thread(target=task,args=(i,)) t.start()
事件(锁,所有锁的释放与锁定)
lock = threading.Event()
lock.wait()
lock.clear()
import threading import time lock = threading.Event() def task(arg): time.sleep(1) # 锁住所有的线程 lock.wait() # 申请使用锁,其他人等 print(arg) for i in range(10): t = threading.Thread(target=task,args=(i,)) t.start() while True: value = input(‘>>>>‘) if value == ‘1‘: lock.set() lock.clear()
指定释放几个线程锁
lock = threading.Condition()
import threading import time lock = threading.Condition() def task(arg): time.sleep(1) # 锁住所有的线程 lock.acquire() lock.wait() # 申请使用锁,其他人等 print(‘线程‘,arg) lock.release() for i in range(10): t = threading.Thread(target=task,args=(i,)) t.start() while True: value = input(‘>>>>‘) lock.acquire() lock.notify(int(value)) #指定释放几个锁 lock.release()
线程池
from concurrent.futures import ThreadPoolExecutor #导入指定模块 import time import requests def task(url): response=requests.get(url) print("请求结果",url,len(response.content)) url_list=[ "http://www.baidu.com", "http://cn.bing.com", "http://www.oldboyedu.com" ] pool=ThreadPoolExecutor(2) #创建线程池 for url in url_list: pool.submit(task,url) #线程池调用
自定义回调函数
from concurrent.futures import ThreadPoolExecutor import time import requests def txt(future): cc=future.result() print(cc) def task(url): response=requests.get(url) return(url,len(response.content)) url_list=[ "http://www.baidu.com", "http://cn.bing.com", "http://www.oldboyedu.com" ] pool=ThreadPoolExecutor(2) for url in url_list: future=pool.submit(task,url) future.add_done_callback(txt)
进程from multiprocessing import Process #导入模块
from multiprocessing import Process import time def task(arg): time.sleep(arg) print(arg) if __name__ == ‘__main__‘: for i in range(10): p = Process(target=task,args=(i,)) p.daemon = True # p.daemon = False p.start() p.join(1) print(‘主进程最后...‘)
进程间内存共享 1、Array
from multiprocessing import Process,Array import time import threading def task(num,li): li[num]=1 print(list(li)) if __name__ == ‘__main__‘: v = Array("i",10) for i in range(10): p = Process(target=task,args=(i,v,)) # p = threading.Thread(target=task,args=(i,v,)) p.start()
2、Manager (内部socket对接连接)
Manager.list() Manager.dict()
from multiprocessing import Process,Manager import time import threading def task(num,li): li.append(num) print(list(li)) if __name__ == ‘__main__‘: dic=Manager().list() for i in range(10): p = Process(target=task,args=(i,dic,)) # p = threading.Thread(target=task,args=(i,v,)) p.start() p.join()
进程池
from concurrent.futures import ProcessPoolExecutor def call(arg): data = arg.result() print(data) def task(arg): print(arg) return arg + 100 if __name__ == ‘__main__‘: pool = ProcessPoolExecutor(5) for i in range(10): obj = pool.submit(task,i) obj.add_done_callback(call)
时间: 2024-11-10 16:14:11