摘要:
from threading import Thread #线程 import time def task(name): print('%s is running' %name) time.sleep(2) if __name__ == '__main__': t1=Thread(target=ta 阅读全文
摘要:
from threading import Thread,Lock import time mutex=Lock() n=100 def task(): global n temp=n time.sleep(0.1) n=temp-1 if __name__ == '__main__': l=[] 阅读全文
摘要:
死锁:此时执行程序中两个或多个线程发生永久堵塞(等待),每个线程都在等待被其他线程占用并堵塞了的资源。 递归锁:可以多次acquire加锁 使用:from threading import RLock mutexA=RLock() from threading import Thread,Lock 阅读全文
摘要:
from threading import Thread,Semaphore import time,random sm=Semaphore(5) def task(name): sm.acquire() #或者with sm print('%s 正在上厕所' %name) time.sleep(r 阅读全文
摘要:
from threading import Thread,Event import time event=Event() #制造一个event事件 def light(): print('红灯正亮着') time.sleep(3) event.set() #绿灯亮 def car(name): pr 阅读全文
摘要:
import queue q=queue.Queue(3) #先进先出 q.put(1) q.put(2) q.put(3) print(q.get()) print(q.get()) print(q.get()) 结果: 1 2 3 import queue q=queue.LifoQueue(3 阅读全文
摘要:
使用:from concurrent.futures import ProcessPoolExecutor from concurrent.futures import ProcessPoolExecutor import time,random,os def task(name): print(' 阅读全文
摘要:
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor import time,random,os def task(name,n): print('%s%s is running' %(name,os.getpid 阅读全文
摘要:
import time def func1(): for i in range(10000000): i+1 def func2(): for i in range(10000000): i+1 start = time.time() func1() func2() stop = time.time 阅读全文
摘要:
from gevent import monkey;monkey.patch_all() from socket import * from gevent import spawn #导入spawn之前一定要传入monkey #俩个io密集型任务 def comun(conn): while Tru 阅读全文