Skip to content

Add checks for if instruments exist before running instrument functions. #628

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

Merged
merged 2 commits into from
Aug 25, 2018
Merged
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
34 changes: 25 additions & 9 deletions trio/_core/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,8 @@ class Runner:
def close(self):
self.io_manager.close()
self.entry_queue.close()
self.instrument("after_run")
if self.instruments:
self.instrument("after_run")

# Methods marked with @_public get converted into functions exported by
# trio.hazmat:
Expand Down Expand Up @@ -718,7 +719,8 @@ def reschedule(self, task, next_send=_NO_SEND):
task._abort_func = None
task.custom_sleep_data = None
self.runq.append(task)
self.instrument("task_scheduled", task)
if self.instruments:
self.instrument("task_scheduled", task)

def spawn_impl(self, async_fn, args, nursery, name, *, system_task=False):

Expand Down Expand Up @@ -853,7 +855,8 @@ def _return_value_looks_like_wrong_library(value):
for scope in nursery._cancel_stack:
scope._add_task(task)

self.instrument("task_spawned", task)
if self.instruments:
self.instrument("task_spawned", task)
# Special case: normally next_send should be a Result, but for the
# very first send we have to send a literal unboxed None.
self.reschedule(task, None)
Expand All @@ -879,7 +882,9 @@ def task_exited(self, task, result):
self.system_nursery.cancel_scope.cancel()
if task is self.init_task:
self.init_task_result = result
self.instrument("task_exited", task)

if self.instruments:
self.instrument("task_exited", task)

################
# System tasks and init
Expand Down Expand Up @@ -1087,6 +1092,9 @@ def abort(_):
################

def instrument(self, method_name, *args):
if not self.instruments:
return

for instrument in list(self.instruments):
try:
method = getattr(instrument, method_name)
Expand Down Expand Up @@ -1279,7 +1287,8 @@ def run(
def run_impl(runner, async_fn, args):
__tracebackhide__ = True

runner.instrument("before_run")
if runner.instruments:
runner.instrument("before_run")
runner.clock.start_clock()
runner.init_task = runner.spawn_impl(
runner.init,
Expand Down Expand Up @@ -1308,9 +1317,13 @@ def run_impl(runner, async_fn, args):
timeout = cushion
idle_primed = True

runner.instrument("before_io_wait", timeout)
if runner.instruments:
runner.instrument("before_io_wait", timeout)

runner.io_manager.handle_io(timeout)
runner.instrument("after_io_wait", timeout)

if runner.instruments:
runner.instrument("after_io_wait", timeout)

now = runner.clock.current_time()
# We process all timeouts in a batch and then notify tasks at the end
Expand Down Expand Up @@ -1359,7 +1372,9 @@ def run_impl(runner, async_fn, args):
while batch:
task = batch.pop()
GLOBAL_RUN_CONTEXT.task = task
runner.instrument("before_task_step", task)

if runner.instruments:
runner.instrument("before_task_step", task)

next_send = task._next_send
task._next_send = None
Expand Down Expand Up @@ -1413,7 +1428,8 @@ def run_impl(runner, async_fn, args):
# and propagate the exception into the task's spawner.
runner.task_exited(task, Error(exc))

runner.instrument("after_task_step", task)
if runner.instruments:
runner.instrument("after_task_step", task)
del GLOBAL_RUN_CONTEXT.task

return runner.init_task_result
Expand Down