我想在同一时间从Django视图上的API发出两个POST请求。Django视图中的Python Asyncio
这就是我在django之外做到的。
import asyncio
import speech_recognition as sr
async def main(language1, language2):
loop = asyncio.get_event_loop()
r = sr.Recognizer()
with sr.AudioFile(path.join(os.getcwd(), "audio.wav")) as source:
audio = r.record(source)
def reco_ibm(lang):
return(r.recognize_ibm(audio, key, secret language=lang, show_all=True))
future1 = loop.run_in_executor(None, reco_ibm, str(language1))
future2 = loop.run_in_executor(None, reco_ibm, str(language2))
response1 = await future1
response2 = await future2
loop = asyncio.get_even_loop()
loop.run_until_complete(main("en-US", "es-ES"))
我对事件循环感到困惑。我如何在Django视图中执行此操作?我是否需要为此使用嵌套函数?
def ibmaudio_ibm(request, language1, language2):
#Asyncio code here
编辑:这甚至被认为是重复的?并行调用和使用crontab进行调度是完全不同的事情......
http://channels.readthedocs.io/en/stable/faqs.html#why-are-you-doing-this-rather-than-just-using-tornado-gevent-asyncio-etc –
可能重复[使用asyncio在django中执行周期性任务](https://stackoverflow.com/questions/43838872/using-asyncio-to-do-periodic-task-in-django) – e4c5
@ e4c5它没有任何关系与调度任务,我想并行呼叫......甚至不接近重复 – Juanvulcano