mirror of
https://github.com/python/cpython.git
synced 2025-11-02 03:01:58 +00:00
bpo-33649: More improvements (GH-9439)
This commit is contained in:
parent
8213eaddf3
commit
e247b46cba
2 changed files with 176 additions and 47 deletions
|
|
@ -755,7 +755,7 @@ Watching file descriptors
|
|||
invoke *callback* with the specified arguments once *fd* is available for
|
||||
writing.
|
||||
|
||||
Use :func:`functools.partial` :ref:`to pass keywords
|
||||
Use :func:`functools.partial` :ref:`to pass keyword arguments
|
||||
<asyncio-pass-keywords>` to *func*.
|
||||
|
||||
.. method:: loop.remove_writer(fd)
|
||||
|
|
@ -969,7 +969,7 @@ Unix signals
|
|||
Raise :exc:`ValueError` if the signal number is invalid or uncatchable.
|
||||
Raise :exc:`RuntimeError` if there is a problem setting up the handler.
|
||||
|
||||
Use :func:`functools.partial` :ref:`to pass keywords
|
||||
Use :func:`functools.partial` :ref:`to pass keyword arguments
|
||||
<asyncio-pass-keywords>` to *func*.
|
||||
|
||||
.. method:: loop.remove_signal_handler(sig)
|
||||
|
|
@ -996,11 +996,52 @@ Executing code in thread or process pools
|
|||
The *executor* argument should be an :class:`concurrent.futures.Executor`
|
||||
instance. The default executor is used if *executor* is ``None``.
|
||||
|
||||
Use :func:`functools.partial` :ref:`to pass keywords
|
||||
<asyncio-pass-keywords>` to *func*.
|
||||
Example::
|
||||
|
||||
import asyncio
|
||||
import concurrent.futures
|
||||
|
||||
def blocking_io():
|
||||
# File operations (such as logging) can block the
|
||||
# event loop: run them in a thread pool.
|
||||
with open('/dev/urandom', 'rb') as f:
|
||||
return f.read(100)
|
||||
|
||||
def cpu_bound():
|
||||
# CPU-bound operations will block the event loop:
|
||||
# in general it is preferable to run them in a
|
||||
# process pool.
|
||||
return sum(i * i for i in range(10 ** 7))
|
||||
|
||||
async def main():
|
||||
loop = asyncio.get_running_loop()
|
||||
|
||||
## Options:
|
||||
|
||||
# 1. Run in the default loop's executor:
|
||||
result = await loop.run_in_executor(
|
||||
None, blocking_io)
|
||||
print('default thread pool', result)
|
||||
|
||||
# 2. Run in a custom thread pool:
|
||||
with concurrent.futures.ThreadPoolExecutor() as pool:
|
||||
result = await loop.run_in_executor(
|
||||
pool, blocking_io)
|
||||
print('custom thread pool', result)
|
||||
|
||||
# 3. Run in a custom process pool:
|
||||
with concurrent.futures.ProcessPoolExecutor() as pool:
|
||||
result = await loop.run_in_executor(
|
||||
pool, cpu_bound)
|
||||
print('custom process pool', result)
|
||||
|
||||
asyncio.run(main())
|
||||
|
||||
This method returns a :class:`asyncio.Future` object.
|
||||
|
||||
Use :func:`functools.partial` :ref:`to pass keyword arguments
|
||||
<asyncio-pass-keywords>` to *func*.
|
||||
|
||||
.. versionchanged:: 3.5.3
|
||||
:meth:`loop.run_in_executor` no longer configures the
|
||||
``max_workers`` of the thread pool executor it creates, instead
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue