@@ -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
@@ -47,10 +48,11 @@ class Runner:
4748
4849 # Note: the class is final, it is not intended for inheritance.
4950
50- def __init__ (self , * , debug = None , loop_factory = None ):
51+ def __init__ (self , * , debug = None , loop_factory = None , eager_tasks = False ):
5152 self ._state = _State .CREATED
5253 self ._debug = debug
5354 self ._loop_factory = loop_factory
55+ self ._eager_tasks = eager_tasks
5456 self ._loop = None
5557 self ._context = None
5658 self ._interrupt_count = 0
@@ -154,6 +156,8 @@ def _lazy_init(self):
154156 self ._loop = self ._loop_factory ()
155157 if self ._debug is not None :
156158 self ._loop .set_debug (self ._debug )
159+ if self ._eager_tasks :
160+ self ._loop .set_task_factory (tasks .eager_task_factory )
157161 self ._context = contextvars .copy_context ()
158162 self ._state = _State .INITIALIZED
159163
@@ -167,7 +171,7 @@ def _on_sigint(self, signum, frame, main_task):
167171 raise KeyboardInterrupt ()
168172
169173
170- def run (main , * , debug = None , loop_factory = None ):
174+ def run (main , * , debug = None , loop_factory = None , eager_tasks = False ):
171175 """Execute the coroutine and return the result.
172176
173177 This function runs the passed coroutine, taking care of
@@ -179,6 +183,7 @@ def run(main, *, debug=None, loop_factory=None):
179183
180184 If debug is True, the event loop will be run in debug mode.
181185 If loop_factory is passed, it is used for new event loop creation.
186+ If eager_tasks is True, the loop creates eager tasks by default.
182187
183188 This function always creates a new event loop and closes it at the end.
184189 It should be used as a main entry point for asyncio programs, and should
@@ -201,7 +206,8 @@ async def main():
201206 raise RuntimeError (
202207 "asyncio.run() cannot be called from a running event loop" )
203208
204- with Runner (debug = debug , loop_factory = loop_factory ) as runner :
209+ with Runner (debug = debug , loop_factory = loop_factory ,
210+ eager_tasks = eager_tasks ) as runner :
205211 return runner .run (main )
206212
207213
0 commit comments