MagicStack / MagicStack/uvloop
why run_coroutine_threadsafe so slow?
Personne n'a encore pris cette issue.
- Langage dominant
- Cython
- Étoiles
- 11.9k
- Forks
- 616
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Description
* **uvloop version**: 0.14.0
* **Python version**: 3.6.5
* **Platform**: centos 6.3 cpu:Intel(R) Xeon(R) Gold 5218 CPU @ 2.30GHz
I have a need to run my code in two loop, demo code like below
```
import threading
import asyncio
import time
import traceback
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
g_loop = asyncio.get_event_loop()
print("g_loop", id(g_loop), g_loop)
async def my_sum():
result = 0
for i in range(1000):
result += 1
async def func():
global g_loop
try:
while True:
s = time.time()
# await my_sum()
future = asyncio.run_coroutine_threadsafe(my_sum(), g_loop)
result = future.result(1)
e = time.time()
print("total", (e - s) * 1000 * 1000)
await asyncio.sleep(0.00001)
except Exception as e:
print("in this======", traceback.format_exc())
def thread_loop():
loop = asyncio.new_event_loop()
print("local loop", id(loop))
task = loop.create_task(func())
loop.run_forever()
t = threading.Thread(target=thread_loop)
t.start()
g_loop.run_forever()
t.join()
```
then, the print time cost about 100us ~ 150us
```
total 107.52677917480469
total 100.13580322265625
total 154.97207641601562
total 144.48165893554688
total 141.62063598632812
total 135.18333435058594
total 174.99923706054688
total 99.89738464355469
total 174.7608184814453
total 102.04315185546875
```
but when I run my code in one loop
```
import threading
import asyncio
import time
import traceback
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
g_loop = asyncio.get_event_loop()
print("g_loop", id(g_loop), g_loop)
async def my_sum():
result = 0
for i in range(1000):
result += 1
async def func():
global g_loop
try:
while True:
s = time.time()
await my_sum()
# future = asyncio.run_coroutine_threadsafe(my_sum(), g_loop)
# result = future.result(1)
e = time.time()
print("total", (e - s) * 1000 * 1000)
await asyncio.sleep(0.00001)
except Exception as e:
print("in this======", traceback.format_exc())
def thread_loop():
loop = asyncio.new_event_loop()
print("local loop", id(loop))
task = loop.create_task(func())
loop.run_forever()
t = threading.Thread(target=thread_loop)
t.start()
g_loop.run_forever()
t.join()
```
the time cost about 40us ~ 80us
```
total 40.0543212890625
total 39.577484130859375
total 39.577484130859375
total 39.577484130859375
total 76.2939453125
total 40.0543212890625
total 39.10064697265625
total 81.53915405273438
total 40.0543212890625
```
Does change a thread need extra 60us ~ 70us? why so slow?
Guide de contribution
Aucun guide de contribution indexé pour ce dépôt
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par exécuter les exemples fournis à deux boucles et à une boucle avec uvloop 0.14.0, Python 3.6.5 et la plateforme indiquée, puis comparez le temps autour de asyncio.run_coroutine_threadsafe et await my_sum(). Le travail est terminé lorsque la source de la latence inter-thread supplémentaire est identifiée ou qu’il est documenté pourquoi elle est attendue, avec une reproduction ou un benchmark ciblé.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- backend
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- À clarifier
- Accessibilité débutants
- 25/100