@@ -27,6 +27,7 @@ class Runner:
2727
2828 If debug is True, the event loop will be run in debug mode.
2929 If loop_factory is passed, it is used for new event loop creation.
30+ If eager_tasks is True, the loop creates eager tasks by default.
3031
3132 asyncio.run(main(), debug=True)
3233
@@ -46,10 +47,11 @@ class Runner:
4647
4748 # Note: the class is final, it is not intended for inheritance.
4849
49- def __init__ (self , * , debug = None , loop_factory = None ):
50+ def __init__ (self , * , debug = None , loop_factory = None , eager_tasks = False ):
5051 self ._state = _State .CREATED
5152 self ._debug = debug
5253 self ._loop_factory = loop_factory
54+ self ._eager_tasks = eager_tasks
5355 self ._loop = None
5456 self ._context = None
5557 self ._interrupt_count = 0
@@ -153,6 +155,8 @@ def _lazy_init(self):
153155 self ._loop = self ._loop_factory ()
154156 if self ._debug is not None :
155157 self ._loop .set_debug (self ._debug )
158+ if self ._eager_tasks :
159+ self ._loop .set_task_factory (tasks .eager_task_factory )
156160 self ._context = contextvars .copy_context ()
157161 self ._state = _State .INITIALIZED
158162
@@ -166,7 +170,7 @@ def _on_sigint(self, signum, frame, main_task):
166170 raise KeyboardInterrupt ()
167171
168172
169- def run (main , * , debug = None , loop_factory = None ):
173+ def run (main , * , debug = None , loop_factory = None , eager_tasks = False ):
170174 """Execute the coroutine and return the result.
171175
172176 This function runs the passed coroutine, taking care of
@@ -178,6 +182,7 @@ def run(main, *, debug=None, loop_factory=None):
178182
179183 If debug is True, the event loop will be run in debug mode.
180184 If loop_factory is passed, it is used for new event loop creation.
185+ If eager_tasks is True, the loop creates eager tasks by default.
181186
182187 This function always creates a new event loop and closes it at the end.
183188 It should be used as a main entry point for asyncio programs, and should
@@ -200,7 +205,8 @@ async def main():
200205 raise RuntimeError (
201206 "asyncio.run() cannot be called from a running event loop" )
202207
203- with Runner (debug = debug , loop_factory = loop_factory ) as runner :
208+ with Runner (debug = debug , loop_factory = loop_factory ,
209+ eager_tasks = eager_tasks ) as runner :
204210 return runner .run (main )
205211
206212
0 commit comments