Skip to main content
12、monkey.patch_all() ?

12、monkey.patch_all() ?

引言:

曾经在使用flask-socket中不添加monkey.patch_all()遇到一些莫名奇妙的错误。所以这里查了些资料关于monkey.patch_all。

一、monkey.patch_all() 是什么?

monkey.patch_all()gevent 库中的一个核心函数,用于在程序启动时动态替换标准库中的某些模块(如 socketthreadingselect 等),使其支持 非阻塞的协程操作
其核心作用是让标准库的阻塞式 I/O 操作(如网络请求、文件读写)在 gevent 的协程(Greenlet)框架下运行,从而实现 高效的异步编程


Y-aong...About 3 minpython协程
8、协程

8、协程

详情还是要看文档

run() 创建事件循环,运行一个协程,关闭事件循环。
Runner A context manager that simplifies multiple async function calls.
Task Task对象
TaskGroup A context manager that holds a group of tasks. Provides a convenient and reliable way to wait for all tasks in the group to finish.
create_task() Start an asyncio Task, then returns it.
current_task() 返回当前Task对象
all_tasks() Return all tasks that are not yet finished for an event loop.
await sleep() 休眠几秒。
await gather() 并发执行所有事件的调度和等待。
await wait_for() 有超时控制的运行。
await shield() 屏蔽取消操作
await wait() 完成情况的监控器
timeout() Run with a timeout. Useful in cases when wait_for is not suitable.
to_thread() 在不同的 OS 线程中异步地运行一个函数。
run_coroutine_threadsafe() 从其他OS线程中调度一个协程。
for in as_completed() for 循环监控完成情况。

Y-aong...About 1 minpython协程