Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Don't shuffle each batch, instead reverse them half the time. #1580

Merged
merged 3 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions trio/_core/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2076,15 +2076,20 @@ def unrolled_run(runner, async_fn, args, host_uses_signal_set_wakeup_fd=False):
# tie-breaker and the non-deterministic ordering of
# task._notify_queues.)
batch = list(runner.runq)
runner.runq.clear()
if _ALLOW_DETERMINISTIC_SCHEDULING:
# We're running under Hypothesis, and pytest-trio has patched
# this in to make the scheduler deterministic and avoid flaky
# tests. It's not worth the (small) performance cost in normal
# operation, since we'll shuffle the list and _r is only
# seeded for tests.
batch.sort(key=lambda t: t._counter)
runner.runq.clear()
_r.shuffle(batch)
palkeo marked this conversation as resolved.
Show resolved Hide resolved
_r.shuffle(batch)
else:
# 50% chance of reversing the batch, this way each task
# can appear before/after any other task.
if _r.random() < 0.5:
batch.reverse()
while batch:
task = batch.pop()
GLOBAL_RUN_CONTEXT.task = task
Expand Down
2 changes: 1 addition & 1 deletion trio/tests/test_scheduler_determinism.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ async def scheduler_trace():
trace = []

async def tracer(name):
for i in range(10):
for i in range(50):
trace.append((name, i))
await trio.sleep(0)

Expand Down