python 进程 线程 协程(通信方式)
发布日期:2021-05-14 22:00:02 浏览次数:10 分类:精选文章

本文共 9997 字,大约阅读时间需要 33 分钟。

������������������������

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������


������������������������

���������������������������������������������������������������������������������������������������������

  • ������������������������������������������CPU������������������������������������������������

  • ������������������������������������������������������������������������������ContextHolder���CPU������������

  • ������������������������������������������������������������������������������������������������������������������������������������������������������������

  • ���������������������������������������������������������������������������������������������������������������������������������������������������������

  • ������������������������������������������������������������������������������������

  • ������������������������������������������������������������������������������������������������������������������������������������

  • ������������������������������������������������������������������������������������������������������������


  • ������������������������������

    ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������CPU���������������������������������������������������������������������������������������������������������������������������

    ���������������������������������������������������������������������������������������������CPU���������������������������������������������������������������������������������������������������������������������


    ���������������������

    ���������������������������I/O���������������������������������������������������������CPU���������������������������������������������������������������������������������������������������������������������������������


    ���������������

    ���Python���������������������threading���������������������������������������������������������������

    import threading
    def 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 threading
    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.setDaemon(True)
    t.start()
    print('main finished...')

    ���������������������������������������

    ������������������������������������������������������������������������������������������������������������������������������������Python���threading���������������������������Lock������������������RLock���������������������������

    import threading
    lock = threading.Lock()
    num = 0
    def 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 threading
    lock = 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 threading
    event = 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 = 0
    thread_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 queue
    q = 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 Queue
    q = 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 threading
    import multiprocessing
    def 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, Process
    q = 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, Pipe
    def 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, os
    import time
    def 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 greenlet
    def 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 gevent
    import time
    def 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������������������

    上一篇:python IO多路复用(异步IO,同步IO,select,poll,epoll)
    下一篇:python web框架 flask(三)

    发表评论

    最新留言

    逛到本站,mark一下
    [***.202.152.39]2025年04月05日 11时34分51秒