Event queue coroutine

Thread queue

FIFO queue

# 1 FIFO队列 queue 先进先出 特殊参数 block=True, timeout=None
import queue
# q=queue.Queue(3)
# q.put(666)
# q.put(777)
# q.put(888)
# print(q.get())
# print(q.get())
# print(q.get())

LIFO 栈

# LIFO 栈. 先进后出 特殊参数 block=True, timeout=None
import queue
# q=queue.LifoQueue(3)
# q.put(666)
# q.put(777)
# q.put(888)
# print(q.get())
# print(q.get())
# print(q.get())

Priority queue

# 优先级对列 需要元组的形式,(int,数据) int 代表优先级,数字越低,优先级越高. get先去重要的
import queue
q=queue.PriorityQueue(3)
q.put((10,'拉级消息'))
q.put((-10,'重要消息'))
q.put((5,'一般消息'))
print(q.get())
print(q.get())
print(q.get())

Event Event

Concurrent perform a task. Multi-threaded multi-process, almost simultaneously executed.

Notify another thread started a thread execution to the middle.

Attributes

# event = Event()  # 默认是False
# event.set()  # 改成了True
# event.wait()  # 轮询检测event是否为True,当其为True,继续下一行代码. 阻塞.
# event.wait(1)
# 设置超时时间,如果1s中以内,event改成True,代码继续执行.
# 设置超时时间,如果超过1s中,event没做改变,代码继续执行.

The first edition useless Event

# 引入 
import time
from threading import Thread
from threading import current_thread
flag = False
def task():
    print(f'{current_thread().name} 检测服务器是否正常开启....')
    time.sleep(3)
    global flag
    flag = True

def task1():
    while 1:
        time.sleep(1)
        print(f'{current_thread().name} 正在尝试连接服务器.....')
        if flag:
            print('连接成功')
            return

if __name__ == '__main__':
    for i in range(4):
        t=Thread(target=task,)
        t.start()
    t4=Thread(target=task1,)
    t4.start()

Version 2 with the Event

import time
from threading import Thread
from threading import current_thread
from threading import Event
def task():
    print(f'{current_thread().name} 检测服务器是否正常开启....')
    time.sleep(3)
    event.set()

def task1():
    while 1:
        time.sleep(1)
        print(f'{current_thread().name} 正在尝试连接服务器.....')
        event.wait()
        print(f'{current_thread().name} 连接成功')

if __name__ == '__main__':
    event = Event()
    for i in range(4):
        t=Thread(target=task,)
        t.start()
    t4=Thread(target=task1,)
    t4.start()

Coroutine acquaintance

A thread concurrency.

Concurrent, parallel, serial:

Serial: When you perform multiple tasks, the first task performed from the beginning, met IO wait, wait for the end of the IO blocking, proceed to the next task.

Parallel: multicore, multiple threads or processes simultaneously execute four cpu, perform four tasks simultaneously.

Concurrency: simultaneously perform multiple tasks looks, cpu switch back and forth between multiple tasks (IO encountered obstruction, compute-intensive execution time is too long).

Concurrent nature:

  1. IO encountered obstruction, compute-intensive execution time is too long to switch.
  2. Original state.

A thread concurrency.

Multi-process: the operating system to control multiple processes multiple tasks switch + hold.

Multithreading: multiple threads operating system to control multiple task switching + hold.

Coroutine: a plurality of program control switching of tasks, and a thread holding state.

Coroutine: Micro concurrent processing tasks should not be excessive.

It coroutine scheduling cpu, if coroutine control tasks, experience blocking, it quickly (faster than the operating system) to switch to another task, and the task can suspend (hold state), so that cpu operating system that has been at work.

Before we learned coroutine? Yield is a coroutine,

Coroutine

Coroutine advantages:

#1. 协程的切换开销更小,属于程序级别的切换,操作系统完全感知不到,因而更加轻量级
#2. 单线程内就可以实现并发的效果,最大限度地利用cpu
#3. 修改共享数据不需加锁

Guess you like

Origin www.cnblogs.com/saoqiang/p/11415408.html