
本文共 9997 字,大约阅读时间需要 33 分钟。
������������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������
���������������������������������������������������������������������������������������������������������
������������������������������������������CPU������������������������������������������������
������������������������������������������������������������������������������ContextHolder���CPU������������
������������������������������������������������������������������������������������������������������������������������������������������������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������������������������������������������������������������������
������������������������������������������������������������������������������������������������������������������������������������
������������������������������������������������������������������������������������������������������������
������������������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������CPU���������������������������������������������������������������������������������������������������������������������������
���������������������������������������������������������������������������������������������CPU���������������������������������������������������������������������������������������������������������������������
���������������������
���������������������������I/O���������������������������������������������������������CPU���������������������������������������������������������������������������������������������������������������������������������
���������������
���Python���������������������threading
���������������������������������������������������������������
import threadingdef run(name): print(name) time.sleep(2)threads = [threading.Thread(target=run, args=(i,)) for i in range(10)]for t in threads: t.start()
���������������������������threading.Thread
������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������join
���������������������������������������������������������������������������������������������join
���������������������������������������������������������.join()
���������������������������������������������������������������
def run(name): for i in range(3): print(name, i) time.sleep(2)threads = [threading.Thread(target=run, args=('t%s' % i,)) for i in range(3)]for t in threads: t.start()for t in threads: t.join()print('main finished...')
������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������
import threadingdef run(name): for i in range(3): print(name, i) time.sleep(2)threads = [threading.Thread(target=run, args=('t%s' % i,)) for i in range(3)]for t in threads: t.setDaemon(True) t.start()print('main finished...')
���������������������������������������
������������������������������������������������������������������������������������������������������������������������������������Python���threading
���������������������������Lock
������������������RLock
���������������������������
import threadinglock = threading.Lock()num = 0def run(name): lock.acquire() global num num += 1 lock.release()threads = [threading.Thread(target=run, args=('t%s' % i,)) for i in range(10)]for t in threads: t.start()for t in threads: t.join()print('num:', num)
���������������������������
���������������������������������������������������������������������������������������������������������������������������������������������multiprocessing.BoundedSemaphore
������������������
import threadinglock = threading.BoundedSemaphore(3)def run(tag): lock.acquire() print(tag) time.sleep(2) lock.release()threads = [threading.Thread(target=run, args=('t%s' % i,)) for i in range(10)]for t in threads: t.start()for t in threads: t.join()print('main finished...')
���������������������
���������������������������������������������������������������������������������������������������������������
import threadingevent = threading.Event()def light(): count = 0 while True: if count < 5: event.set() print('\033[42;1m������...%s\033[0m' % count) elif count > 4: event.clear() print('\033[41;1m������...%s\033[0m' % count) time.sleep(1) count += 1 if count == 10: count = 0thread_light = threading.Thread(target=light)thread_light.start()def car(): while True: if event.is_set(): print('���������������������������') time.sleep(1) else: print('���������,���������������') event.wait()thread_car = threading.Thread(target=car)thread_car.start()
���������Queue���
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
import queueq = queue.Queue(maxsize=2)q.put(1)q.put('domain')try: q.put('alex', block=False)except Exception as e: print(e)print(q.qsize())
���������������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������
from queue import Queueq = Queue(maxsize=10)def produce(): count = 0 while True: q.put(count) print('������%s' % count) count += 1 time.sleep(0.5)def consume(name): while True: data = q.get() time.sleep(1) print('%s���������%s' % (name, data))thread_produce = threading.Thread(target=produce)thread_consume1 = threading.Thread(target=consume, args=('domain,))thread_consume2 = threading.Thread(target=consume, args=('alex',))thread_produce.start()thread_consume1.start()thread_consume2.start()
������
���������������������������������������������������������������������������������������������������������������������������
���������������������
���Python������������������multiprocessing
���������������������������������������������������
import threadingimport multiprocessingdef info(): print('parent process id:', multiprocessing.current_process().pid) print('current process id:', multiprocessing.current_process().pid)def run(): threads = [threading.Thread(target=info) for _ in range(2)] for t in threads: t.start() time.sleep(3)if __name__ == '__main__': info() processes = [multiprocessing.Process(target=run) for _ in range(1)] for p in processes: p.start() p.join() print('master process finished...')
���������������
������������������������������������������������������������������������������������������������������������
from multiprocessing import Queue, Processq = Queue()def fun(arg): arg.put('domain') arg.put('alex')if __name__ == '__main__': q = Queue() p = Process(target=fun, args=(q,)) p.start() print(q.get())
Pipe������
���������������������������������������������������������������������������
from multiprocessing import Process, Pipedef fun(child): child.send(['domain', 'alex']) child.send(['12', '33']) child.close()if __name__ == '__main__': parent, child = Pipe() p = Process(target=fun, args=(child,)) p.start() print(parent.recv()) print(parent.recv()) parent.send("from parent:hello") p.join()
���������
���������������������������������������������������������������������������������������������������������������
from multiprocessing import Pool, osimport timedef run(arg): print('���������:', os.getpid()) time.sleep(2) return os.getpid()if __name__ == '__main__': print('���������id:', os.getpid()) pool = Pool(5) for i in range(20): if i % 2 == 0: pool.apply_async(func=run, args=(i,)) pool.close() pool.join() print('main finished...')
������
������������������������������Greenlet���������������������������������������������������������������������������������������������������������Python���������������Greenlet������������
������������������
from greenlet import greenletdef func1(): print(1) g2.switch() print(2)def func2(): print(3) g1.switch() print(4)g1 = greenlet(func1)g2 = greenlet(func2)g1.switch()
���������������������gevent���
gevent���������������������Greenlet���������������������������API������������������
import geventimport timedef func1(): print('��� func1 ������������������') gevent.sleep(2) print('��� func1 ������������������')def func2(): print('��� func2 ������������������') gevent.sleep(1) print('��� func2 ������������������')start_time = time.time()gevent.joinall([ gevent.spawn(func1), gevent.spawn(func2)])end_time = time.time()print(f'������������{end_time - start_time}')
������
���������������������������������������������������������������������������������������������������������������������������������������������������I/O���������������������������������������������������������������������������������������������������������������������������������������������I/O������������������
发表评论
最新留言
关于作者
