So -
Now thinking on the problem as a whole -
I think maybe a good way to address this is to put the logic on
"counting N interations or X time and allowing switch" - the logic you
had to explicitly mingle in your code in the first example, in
a function that could wrap the iterator of the `for` loop.
However, that idea would need a _synchronous_ call to the loop
to force an async context switch - I don't know if
there is such a call (even an internal one). Actually
I don't know it that is possible - but I can't think of other way of
factoring it out without explicitly triggering an await.
The switcher would then be used just as we use `enumerate` -
think of something along:
for context_switch, data_set in async_switcher(data_sets, timeout=100):
for record in context_switch(data_set):
# the call to __iter__ here would include the logic to decide
whether to switch,
...
And for a single (non-nested) loop, either a separate call or:
for data_set in next(async_switcher(data_sets, timeout=100))[0]:
...
All in all: if there is a valid way to force the async-context switch in an
sync-call to this wrapper object, it is possible to create a small package
that would have this feature and be easy to use.
(And then, we discuss further down if this is stdlib worthy)
On Fri, 14 Jun 2019 at 09:45, Nikita Melentev <[email protected]>
wrote:
> The problem here is that even if I have a coroutine all code between
> «awaits» is blocking.
>
> ``` python
> async def foo():
> data = await connection.get() # it is ok, loop handling request, we
> waiting
> # from here
> for item in data: # this is 10 ** 6 len
> do_sync_jon(item) # this took 1ms
> # to here we are blocking loop for 1 second
> await something_next()
> ```
> _______________________________________________
> Python-ideas mailing list -- [email protected]
> To unsubscribe send an email to [email protected]
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/[email protected]/message/BLLU545Y2FLACLMHC6OVXGJ5YUF66E4K/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/ZKADHNTVHKLUVTW3PPC2RZF5PUXGBFUE/
Code of Conduct: http://python.org/psf/codeofconduct/