asyncio_executor module

class asyncio_executor.AsyncioExecutor(*, loop: typing.Union[asyncio.events.AbstractEventLoop, NoneType] = None, func_executor: typing.Union[concurrent.futures._base.Executor, NoneType] = None) → None[source]

Bases: concurrent.futures._base.Executor

asyncio执行器,可以执行函数或者协程.

[Asyncio executor who can execute the function or coroutine.]

_shutdown

bool

  • 执行器是否终止
[the executor shutdowned or not]
_loop

asyncio.AbstractEventLoop

  • 事件循环
[the eventloop]
_thread

threading.Thread

  • 执行事件循环上任务的线程
[the thread who runs the tasks in the eventloop]
_func_executor

futures.Executor

  • 如果使用执行器执行函数,那么默认使用什么执行器
[which executor will be used by default if must run a function]
shutdown(wait: bool = True, timeout: int = None) → None[source]

关闭执行器

[Close the executor.]

Params:

wait (bool): - 是否等待线程同步 [if waitting for syncing the thread or not.]

timeout (int): - wait为True时才有效果,设置join的等待时间 [set the waitting time.it will take effect only when param wait is True.]]

submit(fn: typing.Any, *args: typing.Any, **kwargs: typing.Any) → concurrent.futures._base.Future[source]

提交任务.

[submit a task.]

会先检查执行器是否已经关闭或者执行器的事件循环是否还在运行. 如果不是则会抛出一个运行时异常 [It will check if the excutor has already closed or the eventloop is not running. If yes, will throw a RuntimeError exception.]

Params:

fn (Union[callable,coroutinefunction]): - 要执行的函数或者协程函数 [the function or coroutinefunction who need to execute]

*args/**kwargs : - fn的参数 [the function’s params]

Returns:
  • 丢进loop后的future对象,因为使用的是`run_coroutine_threadsafe`方法,因此返回的是一个线程安全的`concurrent.futures.Future`对象.

[the instance of Future, because of using the method run_coroutine_threadsafe,it will return a instance of concurrent.futures.Future who is thread safe.]

Return type:(concurrent.futures.Future)

Raise:

(RuntimeError) : - 当执行器是已经关闭或者执行器的事件循环不在运行时,会抛出运行时异常表明无法执行该操作. [when the excutor has already closed or the eventloop is not running.]
asyncio_executor.func_executor_coroutine(func: typing.Any, loop: typing.Union[asyncio.events.AbstractEventLoop, NoneType] = None) → typing.Any[source]

将函数使用`loop.run_in_executor`包装成协程函数.

[wrap loop.run_in_executor as a coroutine function.]

Params:

func (callable) : - 需要使用执行器执行的函数[the function who need the executor to run].

loop (asyncio.AbstractEventLoop) : - 事件循环[the eventloop]

Returns:
  • 执行器的执行结果[the result the func retruned ran in the executor]
Return type:(Any)