我想在同一時間從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