@@ -616,7 +616,8 @@ class Runner:
616
616
def close (self ):
617
617
self .io_manager .close ()
618
618
self .entry_queue .close ()
619
- self .instrument ("after_run" )
619
+ if self .instruments :
620
+ self .instrument ("after_run" )
620
621
621
622
# Methods marked with @_public get converted into functions exported by
622
623
# trio.hazmat:
@@ -718,7 +719,8 @@ def reschedule(self, task, next_send=_NO_SEND):
718
719
task ._abort_func = None
719
720
task .custom_sleep_data = None
720
721
self .runq .append (task )
721
- self .instrument ("task_scheduled" , task )
722
+ if self .instruments :
723
+ self .instrument ("task_scheduled" , task )
722
724
723
725
def spawn_impl (self , async_fn , args , nursery , name , * , system_task = False ):
724
726
@@ -853,7 +855,8 @@ def _return_value_looks_like_wrong_library(value):
853
855
for scope in nursery ._cancel_stack :
854
856
scope ._add_task (task )
855
857
856
- self .instrument ("task_spawned" , task )
858
+ if self .instruments :
859
+ self .instrument ("task_spawned" , task )
857
860
# Special case: normally next_send should be a Result, but for the
858
861
# very first send we have to send a literal unboxed None.
859
862
self .reschedule (task , None )
@@ -879,7 +882,9 @@ def task_exited(self, task, result):
879
882
self .system_nursery .cancel_scope .cancel ()
880
883
if task is self .init_task :
881
884
self .init_task_result = result
882
- self .instrument ("task_exited" , task )
885
+
886
+ if self .instruments :
887
+ self .instrument ("task_exited" , task )
883
888
884
889
################
885
890
# System tasks and init
@@ -1087,6 +1092,9 @@ def abort(_):
1087
1092
################
1088
1093
1089
1094
def instrument (self , method_name , * args ):
1095
+ if not self .instruments :
1096
+ return
1097
+
1090
1098
for instrument in list (self .instruments ):
1091
1099
try :
1092
1100
method = getattr (instrument , method_name )
@@ -1279,7 +1287,8 @@ def run(
1279
1287
def run_impl (runner , async_fn , args ):
1280
1288
__tracebackhide__ = True
1281
1289
1282
- runner .instrument ("before_run" )
1290
+ if runner .instruments :
1291
+ runner .instrument ("before_run" )
1283
1292
runner .clock .start_clock ()
1284
1293
runner .init_task = runner .spawn_impl (
1285
1294
runner .init ,
@@ -1308,9 +1317,13 @@ def run_impl(runner, async_fn, args):
1308
1317
timeout = cushion
1309
1318
idle_primed = True
1310
1319
1311
- runner .instrument ("before_io_wait" , timeout )
1320
+ if runner .instruments :
1321
+ runner .instrument ("before_io_wait" , timeout )
1322
+
1312
1323
runner .io_manager .handle_io (timeout )
1313
- runner .instrument ("after_io_wait" , timeout )
1324
+
1325
+ if runner .instruments :
1326
+ runner .instrument ("after_io_wait" , timeout )
1314
1327
1315
1328
now = runner .clock .current_time ()
1316
1329
# We process all timeouts in a batch and then notify tasks at the end
@@ -1359,7 +1372,9 @@ def run_impl(runner, async_fn, args):
1359
1372
while batch :
1360
1373
task = batch .pop ()
1361
1374
GLOBAL_RUN_CONTEXT .task = task
1362
- runner .instrument ("before_task_step" , task )
1375
+
1376
+ if runner .instruments :
1377
+ runner .instrument ("before_task_step" , task )
1363
1378
1364
1379
next_send = task ._next_send
1365
1380
task ._next_send = None
@@ -1413,7 +1428,8 @@ def run_impl(runner, async_fn, args):
1413
1428
# and propagate the exception into the task's spawner.
1414
1429
runner .task_exited (task , Error (exc ))
1415
1430
1416
- runner .instrument ("after_task_step" , task )
1431
+ if runner .instruments :
1432
+ runner .instrument ("after_task_step" , task )
1417
1433
del GLOBAL_RUN_CONTEXT .task
1418
1434
1419
1435
return runner .init_task_result
0 commit comments