Skip to content

Deterministic scheduling #2022

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
mandolaerik opened this issue May 27, 2021 · 1 comment
Closed

Deterministic scheduling #2022

mandolaerik opened this issue May 27, 2021 · 1 comment

Comments

@mandolaerik
Copy link

It would be nice with a documented way to make scheduling deterministic when running in guest mode. I have a use case with no I/O, where we value deterministic behavior.

Example: The following program sometimes prints 0 1 2 3 4, and sometimes prints 4 3 2 1 0.

from queue import SimpleQueue, Empty
import trio

class Loop:
    def __init__(self):
        self.q = SimpleQueue()
    def process_work(self):
        while True:
            try:
                f = self.q.get_nowait()
            except Empty:
                return
            f()

loop = Loop()

async def task(i):
    print(i)
async def run_nursery():
    async with trio.open_nursery() as nursery:
        for i in range(5):
            nursery.start_soon(task, i)

trio.lowlevel.start_guest_run(
    run_nursery,
    run_sync_soon_threadsafe=loop.q.put,
    done_callback=lambda _: None)
loop.process_work()

I have a hideously crude workaround to suppress the problem for now:

trio._core._run._r.random = lambda: 1

but it would be nice with a supported way to do this.

@Zac-HD
Copy link
Member

Zac-HD commented Mar 17, 2023

This is a big design question, discussed in #32.

@Zac-HD Zac-HD closed this as completed Mar 17, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants