trio icon indicating copy to clipboard operation
trio copied to clipboard

Please allow keyword arguments of deterministic scheduling and random-value generation

Open joelb123 opened this issue 2 years ago • 2 comments

Deterministic scheduling is a very desirable characteristic for user code testing where external interactions can be mocked-up. In my view, this is one of the major advantages of trio over asyncio. Deterministic scheduling is controlled by the following two lines in core/_run.py:

_ALLOW_DETERMINISTIC_SCHEDULING: FinalT = False _r = random.Random()

Right now these are monkey-patched by pytest-trio, but not all user code uses pytest-trio to create test runners (for example, anyio). Downstream users are thus left to monkey-patch the runner factory themselves.

It would be nice if Trio's constructor had these as keyword arguments. I think separate arguments would be best, because I can think of cases where one might wish to control random-value generation without incurring the performance hit of deterministic scheduling.

joelb123 avatar Jun 29 '23 17:06 joelb123

They're not actually handled by pytest-trio; precisely for this kind of compatibility the Hypothesis-supporting code was moved into Trio itself in https://github.com/python-trio/trio/issues/2981.

However, I don't think it's worth adding top-level arguments which puport to make things deterministic until we've addressed the I/O challenges listed in https://github.com/python-trio/trio/issues/3089 and then any other problems that we discover in the process.

Zac-HD avatar Oct 03 '24 02:10 Zac-HD

#3089 sounds fairly ambitious, and it even talks about wanting a separate flag to enable anyway, so given the requests for it I think we could at least make it easier to enable mostly-deterministic scheduling instead of having people do sketchy monkeypatching. Whether that's a top-level argument with an explicitly sketchy name like enable_mostly_deterministic_scheduling=True or a private global variable (maybe in trio.testing?) that's semi-publicly documented to be the ""proper"" way of doing it.

jakkdl avatar Oct 31 '24 10:10 jakkdl