没并发测试过
具体方法是动态添加协程; 动态添加协程
只是给出一个思路:
就只放视图函数了, 路由那些自己随意配把;
下面代码中用了aiomysql
from django.http import HttpRequest,HttpResponse
from django.shortcuts import render,redirect
import aiomysql
import asyncio
import threading
import time
thread_handler = None
from concurrent.futures import ThreadPoolExecutor,as_completed
#创建一个ioloop给线程用
lp = asyncio.new_event_loop() #type:asyncio.AbstractEventLoop
#mysql pool
pool = None
#线程函数, 在线程中启动一个ioloop
def ioloop(lp : asyncio.AbstractEventLoop):
print("thread id:" , threading.currentThread().ident)
asyncio.set_event_loop(lp)
lp.run_forever()
#启动线程
def start_ioloop():
thread_handler = threading.Thread(target=ioloop,args=(lp,))
thread_handler.start()
#这里用了aiomysql
fu = asyncio.run_coroutine_threadsafe(init_mysql_pool(lp),lp)