threading

Python 多线程多进程

python, multiprocessing, threading, asyncio

一个协程示例: import asyncio async def my_coroutine(): # 这里是协程的代码 await asyncio.sleep(1) print("Coroutine is done") async def main(): # 创建一个任务对象 task = asyncio.create_task(my_coroutine()) # 等待任务完成 await task # 运行主协程 asyncio.run(main()) 一个线程示例: import threading # 定义一个函数,用于在线程中执行 def thread_function(name): print(f"Thread {name}: starting") # 在这里执行一些操作 print(f"Thread {name}: finishing") # 创建线程的实例 thread = threading.Thread(target=thread_function, args=(1,)) # 启动线程 thread.start() # 等待线程结束 thread.join() print("Main thread is done") 一个loop tip: self.loop = asyncio.get_event_loop() self.queue.set_loop(loop=self.loop) await self.af.set_loop(loop=self.loop) await self.adjust.set_loop(loop=self.loop) 为什么这里使用同一个loop ? ...