@@ -197,6 +197,7 @@ def __init__(self):
197197 # exceed this duration in seconds, the slow callback/task is logged.
198198 self .slow_callback_duration = 0.1
199199 self ._current_handle = None
200+ self ._task_factory = None
200201
201202 def __repr__ (self ):
202203 return ('<%s running=%s closed=%s debug=%s>'
@@ -209,11 +210,32 @@ def create_task(self, coro):
209210 Return a task object.
210211 """
211212 self ._check_closed ()
212- task = tasks .Task (coro , loop = self )
213- if task ._source_traceback :
214- del task ._source_traceback [- 1 ]
213+ if self ._task_factory is None :
214+ task = tasks .Task (coro , loop = self )
215+ if task ._source_traceback :
216+ del task ._source_traceback [- 1 ]
217+ else :
218+ task = self ._task_factory (self , coro )
215219 return task
216220
221+ def set_task_factory (self , factory ):
222+ """Set a task factory that will be used by loop.create_task().
223+
224+ If factory is None the default task factory will be set.
225+
226+ If factory is a callable, it should have a signature matching
227+ '(loop, coro)', where 'loop' will be a reference to the active
228+ event loop, 'coro' will be a coroutine object. The callable
229+ must return a Future.
230+ """
231+ if factory is not None and not callable (factory ):
232+ raise TypeError ('task factory must be a callable or None' )
233+ self ._task_factory = factory
234+
235+ def get_task_factory (self ):
236+ """Return a task factory, or None if the default one is in use."""
237+ return self ._task_factory
238+
217239 def _make_socket_transport (self , sock , protocol , waiter = None , * ,
218240 extra = None , server = None ):
219241 """Create socket transport."""
@@ -465,25 +487,25 @@ def call_soon_threadsafe(self, callback, *args):
465487 self ._write_to_self ()
466488 return handle
467489
468- def run_in_executor (self , executor , callback , * args ):
469- if (coroutines .iscoroutine (callback )
470- or coroutines .iscoroutinefunction (callback )):
490+ def run_in_executor (self , executor , func , * args ):
491+ if (coroutines .iscoroutine (func )
492+ or coroutines .iscoroutinefunction (func )):
471493 raise TypeError ("coroutines cannot be used with run_in_executor()" )
472494 self ._check_closed ()
473- if isinstance (callback , events .Handle ):
495+ if isinstance (func , events .Handle ):
474496 assert not args
475- assert not isinstance (callback , events .TimerHandle )
476- if callback ._cancelled :
497+ assert not isinstance (func , events .TimerHandle )
498+ if func ._cancelled :
477499 f = futures .Future (loop = self )
478500 f .set_result (None )
479501 return f
480- callback , args = callback ._callback , callback ._args
502+ func , args = func ._callback , func ._args
481503 if executor is None :
482504 executor = self ._default_executor
483505 if executor is None :
484506 executor = concurrent .futures .ThreadPoolExecutor (_MAX_WORKERS )
485507 self ._default_executor = executor
486- return futures .wrap_future (executor .submit (callback , * args ), loop = self )
508+ return futures .wrap_future (executor .submit (func , * args ), loop = self )
487509
488510 def set_default_executor (self , executor ):
489511 self ._default_executor = executor
0 commit comments