0
streamSimulation
を2スレッド間で4回に分けて呼び出すことにします。異なるスレッドでコルーチンループを実行する
2番目のループを作成し、2番目のスレッドを作成してそのスレッドでループを実行するにはどうすればよいですか?
import asyncio
import functools
from concurrent.futures import ThreadPoolExecutor
async def streamSimulation(p1,p2,p3,p4):
print("Stream init")
while True:
await asyncio.sleep(2)
print("Stream Simulation")
print("Params: " + p1 + p2 + p3 + p4)
doSomething()
def doSomething():
print("Did something")
def main():
loop = asyncio.get_event_loop()
#Supposed to run in first thread
asyncio.ensure_future(streamSimulation("P1","P2","P3","P4"))
asyncio.ensure_future(streamSimulation("A1","A2","A3","A4"))
#Supposed to run in second thread
asyncio.ensure_future(streamSimulation("Q1","Q2","Q3","Q4"))
asyncio.ensure_future(streamSimulation("B1","B2","B3","B4"))
loop.run_forever()
main()
私はいくつかのWebソケットを聞いて、何かをDBに書き込もうとしています。これはIOですか? – Arne
なぜ同じイベントループでいくつかのWebソケットを聞くことができないのですか? [aiohttp](https://docs.aiohttp.org/en/stable/)はそれを行うことができます。 asyncioには、サポートされているTCPポート量の制限はありません。 –
パフォーマンスの理由。私は同時に大量のトラフィックが多いウェブソケットを聞いて、いくつかの値を集計してDBに送ります。私は、マルチスレッド化がGILのためにパフォーマンスを向上させることはないことに気付きました。今はマルチプロセッシングを見て、ProcessPoolExecutorのように見えるかもしれません。しかし、 'run_in_executor'でコルーチンを実行することはできないようです。現在websocketsパッケージを使用しています。 – Arne