'cannot reuse already awaited coroutine' with timed_window on Python 3.7.5

Aperta
#284 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
python

Direzione di ricerca

Start with the timed_window path in streamz/core.py around line 917 and the Sink.update method around line 535. Reproduce the Python 3.7.5 example, then inspect tests/core.py for related asynchronous sink coverage. Done means a regression test demonstrates the repeated-await failure and the chosen fix preserves existing tests.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Using timed_window in Python 3.7.5 can sometimes lead to a coroutine being awaited more than once.

Here's a sample program:

async def my_sink(x):
    print(x)
    await sleep(2)
    # raise Exception("Blah!")


async def main():
    source = Stream(asynchronous=True)
    source.timed_window(interval=1).sink(my_sink)

    for x in range(100):
        await source.emit(x)
        await sleep(0.2)


if __name__ == "__main__":
    run(main(), debug=True)

I narrowed down the reason to the same coroutine being possibly returned more than once in https://github.com/python-streamz/streamz/blob/master/streamz/core.py#L917.

A way to work around this issue is to create a task that runs the coroutine. Modifying the update method of the Sink class to return a task fixes the issue. The tests in tests/core.py continue pass.

diff --git a/streamz/core.py b/streamz/core.py
index fe588ed..6916f19 100644
--- a/streamz/core.py
+++ b/streamz/core.py
@@ -535,7 +535,7 @@ class sink(Stream):
     def update(self, x, who=None):
         result = self.func(x, *self.args, **self.kwargs)
         if gen.isawaitable(result):
-            return result
+            return gen.convert_yielded(result)
         else:
             return []

I'm quite sure this can break something else. What am I missing?

Also, how can we go about writing a test case that demonstrates the issue?

Lingua principale
Python
Stelle
1.3k
Fork
149
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di python-streamz/streamz

Tutte le issue di python-streamz/streamz

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.