Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Laura F. D.
@Fuyukai
a compatability lib that lerts you write code once and have it work with all of trio, curio and asyncio
Jörn Heissler
@joernheissler
That means the work I'm currently putting into writing a lib that's based on trio is a waste of time? :-(
Laura F. D.
@Fuyukai
no, anyone using trio could then use that
Michael Elsdörfer
@miracle2k
Am I wrong to expect it to be akin to multio, but not global-scoped?
Michael Elsdörfer
@miracle2k
Is there a way to implement __await__ in trio. For example, to make trio.Lock work in a similar way as asyncio.Lock? with await lock?
Matthias Urlichs
@smurfix
@miracle2k I don't understand your problem. In trio, "async with lock:" works perfectly, just like w/ asyncio.
Matthias Urlichs
@smurfix

Otherwise of course you can implement your own __await__, thus

import trio
class Foo:
    async def baz(self):
        return 42
    def __await__(self):
        return self.baz().__await__()

async def foo():
    f = Foo()
    x = await f
    return x
print(trio.run(foo))

prints 42

Michael Elsdörfer
@miracle2k
@smurfix apparantly, in asyncio you can do with await lock as well ; for a minute there I wanted to match that syntax in trio, but it's all water under the bridge. thanks for the await() trick!
Jörn Heissler
@joernheissler
For Lock I think it's acceptable to not have an __await__ function because it's best used as a context manager. But what is the rationale to not add the function to e.g. trio.Event?
Nathaniel J. Smith
@njsmith
@joernheissler part of trio's "house style" is that we pretend await async_fn_or_method(...) is the only legal syntax, and await obj doesn't exist. This simplifies teaching, and writing await obj.<operation>() almost always leads to clearer code anyway.
Jörn Heissler
@joernheissler
okay!
Nathaniel J. Smith
@njsmith
This does mean slightly less terse code for Event, but since Event is basically the only thing that would really benefit, I don't think it's worth making everyone learn about the awaitable object protocol just for that :-)
Jörn Heissler
@joernheissler
Maybe some others like ParkingLot too. But if that's the style you're following, then I'm fine with it.
Personally I prefer terse code even if it means more learning.
Nathaniel J. Smith
@njsmith
Yeah, there are always tradeoffs, and everyone draws their lines in different places :-)
Jörn Heissler
@joernheissler
For example in C I think that if (!a + !b + !c == 1) { } is perfectly fine code :-)
hmm.. even works in python. I didn't know one could sum up bools.
Nathaniel J. Smith
@njsmith
But, trio's pretty terse already (cf the 10x shorter happy eyeballs!), so I'm ok with missing out on 1% terseness optimizations here and there :-)
Yeah, python bools are literally just the numbers 0 and 1 with a subclass to change how they print
issubclass(bool, int) is True
Kyle Lawlor
@wgwz
:wave: hi, everyone. i been around here a bit recently. i'm looking to use trio and concurrent programming for dealing with pagination in api endpoints with some projects i'm working on. got a little mcve together for testing making async requests to a walmart api (plenty of data). that said, i've already hit a rate-limit doing the async requests! i'm now wondering what type of things i can use within trio to say wait a little bit if i'm rate-limited. (sharing some code soon)
Nathaniel J. Smith
@njsmith
@wgwz well, there's trio.sleep... not sure if that's what you're asking though :-)
Kyle Lawlor
@wgwz
just was going to the docs for that @njsmith, and i'm not exactly sure what i'm asking for yet. having fun and learning a lot with trio though :)
Nathaniel J. Smith
@njsmith
cool :-)
Kyle Lawlor
@wgwz
well so far i can say that the docs advice about not forgetting to use the await keyword is very helpful (recall that somewhere in the docs). trio.sleep was not working as expected but then i realized ah, right, this needs an await.
Nathaniel J. Smith
@njsmith
yeah
Kyle Lawlor
@wgwz
trio.sleep definitely solves it for me. it turns out the walmart api only accepts 5 requests/second, so i just trio.sleep(1) after opening 5 connections, and then open 5 more.
it was a little tricky to figure out where to place the sleep, but i think that's me just being unused to these tools.
Nathaniel J. Smith
@njsmith
there was a bit of a kerfluffle about this at the python sprint last week, when one of the official python bots posted a github comment like "Thanks <coroutine object get_username at 0x7f8cebe000f8> for your contribution!"... unfortunately in asyncio it's essentially impossible to have a mechanism to detect automatically when an await is missing, and that makes it difficult to argue for changing the interpreter to let trio programs detect it :-/
Kyle Lawlor
@wgwz
what about taking a mypy-like approach? or a trio-linter?
Nathaniel J. Smith
@njsmith
I don't think I could implement such a thing, because I don't know much about static analysis, but it would certainly be helpful
I guess it would actually have to be part of mypy or similar, because it would require fairly deep analysis to know which functions/methods were async
Kyle Lawlor
@wgwz
I certainly don't know enough to implement either, just an idea :-)
Nathaniel J. Smith
@njsmith
also it'd probably be more useful for trio than for asyncio, which might reduce the chances of the main mypy devs spontaneously implementing it
but dunno, maybe if someone fearlessly dug into mypy it would turn out to be not so hard after all :-)
or pylint, maybe
I also have an interpreter patch to let trio detect it mostly-reliably at runtime, but it didn't get into python 3.7 so it's 3.8 at the earliest
Kyle Lawlor
@wgwz
Is there a place to view the patch? I'm curious to take a look, that will be super helpful.
Here's my proof-of-concept for paginated requests (for anyone interested): https://gist.github.com/wgwz/7c4b0a4e2fb10dbb67a84600e9cf0208
(always glad for feedback as well)
Nathaniel J. Smith
@njsmith
the idea of the patch is to add a switch to the interpreter (which trio would turn on), where it keeps track of all coroutine objects that haven't been awaited. Then whenever trio switches tasks, it would ask the interpreter whether this list was non-empty, and if so then it would know that some code in this task had forgotten an await, and raise an error
so it's not quite as nice as raising an immediate TypeError at the point where you forget the await, but it's a lot better than nothing
Kyle Lawlor
@wgwz
I agree! That would have been very helpful for me tonight :-) but even beyond that it would probably save people a lot time debugging. It's an awfully easy mistake to make as noted in the docs.
Nathaniel J. Smith
@njsmith
yeah
well, we will do what we can :-)
Kyle Lawlor
@wgwz
sgtm :-) i'm having lots of fun with trio
Jörn Heissler
@joernheissler
Does the race function in https://trio.readthedocs.io/en/latest/reference-core.html#custom-supervisors suffer from a race condition, i.e. two or more async_fns may finish before getting cancelled?
I was considering to use the snippet to read from multiple queues. But I think this might cause items to get lost.
Matthias Urlichs
@smurfix
@joernheissler I assume that you want to get those items during the next pass.
Jörn Heissler
@joernheissler
I wouldn't want to remove them from the queue