Skip to content

Commit c13c60e

Browse files
committed
Rename run_sync_in_worker_thread -> run_sync_in_thread
See python-triogh-810
1 parent 13d27d6 commit c13c60e

24 files changed

+102
-83
lines changed

docs/source/history.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ Upcoming breaking changes with warnings (i.e., stuff that in 0.2.0
560560
functions that take and run a synchronous function. As part of this:
561561

562562
* ``run_in_worker_thread`` is becoming
563-
:func:`run_sync_in_worker_thread`
563+
``run_sync_in_worker_thread``
564564

565565
* We took the opportunity to refactor ``run_in_trio_thread`` and
566566
``await_in_trio_thread`` into the new class
@@ -655,7 +655,7 @@ CPython, or PyPy3 5.9+.
655655
Other changes
656656
~~~~~~~~~~~~~
657657

658-
* :func:`run_sync_in_worker_thread` now has a :ref:`robust mechanism
658+
* :func:`run_sync_in_thread` now has a :ref:`robust mechanism
659659
for applying capacity limits to the number of concurrent threads
660660
<worker-thread-limiting>` (`#10
661661
<https://github.com/python-trio/trio/issues/170>`__, `#57

docs/source/reference-core.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,7 @@ In acknowledgment of this reality, Trio provides two useful utilities
15661566
for working with real, operating-system level,
15671567
:mod:`threading`\-module-style threads. First, if you're in Trio but
15681568
need to push some blocking I/O into a thread, there's
1569-
:func:`run_sync_in_worker_thread`. And if you're in a thread and need
1569+
:func:`run_sync_in_thread`. And if you're in a thread and need
15701570
to communicate back with Trio, you can use a
15711571
:class:`BlockingTrioPortal`.
15721572

@@ -1589,7 +1589,7 @@ are spawned and the system gets overloaded and crashes. Instead, the N
15891589
threads start executing the first N jobs, while the other
15901590
(100,000 - N) jobs sit in a queue and wait their turn. Which is
15911591
generally what you want, and this is how
1592-
:func:`trio.run_sync_in_worker_thread` works by default.
1592+
:func:`trio.run_sync_in_thread` works by default.
15931593

15941594
The downside of this kind of thread pool is that sometimes, you need
15951595
more sophisticated logic for controlling how many threads are run at
@@ -1636,10 +1636,10 @@ re-using threads, but has no admission control policy: if you give it
16361636
responsible for providing the policy to make sure that this doesn't
16371637
happen – but since it *only* has to worry about policy, it can be much
16381638
simpler. In fact, all there is to it is the ``limiter=`` argument
1639-
passed to :func:`run_sync_in_worker_thread`. This defaults to a global
1639+
passed to :func:`run_sync_in_thread`. This defaults to a global
16401640
:class:`CapacityLimiter` object, which gives us the classic fixed-size
16411641
thread pool behavior. (See
1642-
:func:`current_default_worker_thread_limiter`.) But if you want to use
1642+
:func:`current_default_thread_limiter`.) But if you want to use
16431643
"separate pools" for type A jobs and type B jobs, then it's just a
16441644
matter of creating two separate :class:`CapacityLimiter` objects and
16451645
passing them in when running these jobs. Or here's an example of
@@ -1679,7 +1679,7 @@ time::
16791679
return USER_LIMITERS[user_id]
16801680
except KeyError:
16811681
per_user_limiter = trio.CapacityLimiter(MAX_THREADS_PER_USER)
1682-
global_limiter = trio.current_default_worker_thread_limiter()
1682+
global_limiter = trio.current_default_thread_limiter()
16831683
# IMPORTANT: acquire the per_user_limiter before the global_limiter.
16841684
# If we get 100 jobs for a user at the same time, we want
16851685
# to only allow 3 of them at a time to even compete for the
@@ -1690,17 +1690,17 @@ time::
16901690

16911691

16921692
async def run_in_worker_thread_for_user(user_id, async_fn, *args, **kwargs):
1693-
# *args belong to async_fn; **kwargs belong to run_sync_in_worker_thread
1693+
# *args belong to async_fn; **kwargs belong to run_sync_in_thread
16941694
kwargs["limiter"] = get_user_limiter(user_id)
1695-
return await trio.run_sync_in_worker_thread(asycn_fn, *args, **kwargs)
1695+
return await trio.run_sync_in_thread(asycn_fn, *args, **kwargs)
16961696

16971697

16981698
Putting blocking I/O into worker threads
16991699
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17001700

1701-
.. autofunction:: run_sync_in_worker_thread
1701+
.. autofunction:: run_sync_in_thread
17021702

1703-
.. autofunction:: current_default_worker_thread_limiter
1703+
.. autofunction:: current_default_thread_limiter
17041704

17051705

17061706
Getting back into the Trio thread from another thread

docs/source/reference-core/blocking-trio-portal-example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async def main():
2222
# In a background thread, run:
2323
# thread_fn(portal, receive_from_trio, send_to_trio)
2424
nursery.start_soon(
25-
trio.run_sync_in_worker_thread,
25+
trio.run_sync_in_thread,
2626
thread_fn, portal, receive_from_trio, send_to_trio
2727
)
2828

docs/source/reference-hazmat.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ This logic is a bit convoluted, but accomplishes all of the following:
442442
loop outside of the ``except BlockingIOError:`` block.
443443

444444
These functions can also be useful in other situations. For example,
445-
when :func:`trio.run_sync_in_worker_thread` schedules some work to run
445+
when :func:`trio.run_sync_in_thread` schedules some work to run
446446
in a worker thread, it blocks until the work is finished (so it's a
447447
schedule point), but by default it doesn't allow cancellation. So to
448448
make sure that the call always acts as a checkpoint, it calls

docs/source/reference-io.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ To understand why, you need to know two things.
479479
First, right now no mainstream operating system offers a generic,
480480
reliable, native API for async file or filesystem operations, so we
481481
have to fake it by using threads (specifically,
482-
:func:`run_sync_in_worker_thread`). This is cheap but isn't free: on a
482+
:func:`run_sync_in_thread`). This is cheap but isn't free: on a
483483
typical PC, dispatching to a worker thread adds something like ~100 µs
484484
of overhead to each operation. ("µs" is pronounced "microseconds", and
485485
there are 1,000,000 µs in a second. Note that all the numbers here are

newsfragments/810.removal.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``run_sync_in_worker_thread`` was too much of a mouthful – now it's
2+
just called `run_sync_in_thread` (though the old name still works with
3+
a deprecation warning, for now). Similarly,
4+
``current_default_worker_thread_limiter`` is becoming
5+
`current_default_thread_limiter`.

notes-to-self/blocking-read-hack.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ async def kill_it_after_timeout(new_fd):
2929
async with trio.open_nursery() as nursery:
3030
nursery.start_soon(kill_it_after_timeout, new_fd)
3131
try:
32-
data = await trio.run_sync_in_worker_thread(os.read, new_fd, count)
32+
data = await trio.run_sync_in_thread(os.read, new_fd, count)
3333
except OSError as exc:
3434
if cancel_requested and exc.errno == errno.ENOTCONN:
3535
# Call was successfully cancelled. In a real version we'd

notes-to-self/thread-dispatch-bench.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# minimal a fashion as possible.
33
#
44
# This is useful to get a sense of the *lower-bound* cost of
5-
# run_sync_in_worker_thread
5+
# run_sync_in_thread
66

77
import threading
88
from queue import Queue

trio/__init__.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
)
3333

3434
from ._threads import (
35-
run_sync_in_worker_thread, current_default_worker_thread_limiter,
35+
run_sync_in_thread, current_default_thread_limiter,
3636
BlockingTrioPortal
3737
)
3838

@@ -100,6 +100,20 @@
100100
"library 'subprocess' module"
101101
),
102102
),
103+
"run_sync_in_worker_thread":
104+
_deprecate.DeprecatedAttribute(
105+
run_sync_in_thread,
106+
"0.12.0",
107+
issue=810,
108+
instead=run_sync_in_thread,
109+
),
110+
"current_default_worker_thread_limiter":
111+
_deprecate.DeprecatedAttribute(
112+
current_default_thread_limiter,
113+
"0.12.0",
114+
issue=810,
115+
instead=current_default_thread_limiter,
116+
),
103117
}
104118

105119
# Having the public path in .__module__ attributes is important for:

trio/_core/_entry_queue.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class TrioToken:
132132
133133
1. It lets you re-enter the Trio run loop from external threads or signal
134134
handlers. This is the low-level primitive that
135-
:func:`trio.run_sync_in_worker_thread` uses to receive results from
135+
:func:`trio.run_sync_in_thread` uses to receive results from
136136
worker threads, that :func:`trio.open_signal_receiver` uses to receive
137137
notifications about signals, and so forth.
138138

trio/_core/_traps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def abort_func(raise_cancel):
111111
At that point there are again two possibilities. You can simply ignore
112112
the cancellation altogether: wait for the operation to complete and
113113
then reschedule and continue as normal. (For example, this is what
114-
:func:`trio.run_sync_in_worker_thread` does if cancellation is disabled.)
114+
:func:`trio.run_sync_in_thread` does if cancellation is disabled.)
115115
The other possibility is that the ``abort_func`` does succeed in
116116
cancelling the operation, but for some reason isn't able to report that
117117
right away. (Example: on Windows, it's possible to request that an

trio/_core/tests/test_run.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from .tutil import check_sequence_matches, gc_collect_harder
1919
from ... import _core
20-
from ..._threads import run_sync_in_worker_thread
20+
from ..._threads import run_sync_in_thread
2121
from ..._timeouts import sleep, fail_after
2222
from ..._util import aiter_compat
2323
from ...testing import (
@@ -552,7 +552,7 @@ async def test_cancel_scope_repr(mock_clock):
552552
scope.deadline = _core.current_time() + 10
553553
assert "deadline is 10.00 seconds from now" in repr(scope)
554554
# when not in async context, can't get the current time
555-
assert "deadline" not in await run_sync_in_worker_thread(repr, scope)
555+
assert "deadline" not in await run_sync_in_thread(repr, scope)
556556
scope.cancel()
557557
assert "cancelled" in repr(scope)
558558
assert "exited" in repr(scope)

trio/_file_io.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
class AsyncIOWrapper(AsyncResource):
5454
"""A generic :class:`~io.IOBase` wrapper that implements the :term:`asynchronous
5555
file object` interface. Wrapped methods that could block are executed in
56-
:meth:`trio.run_sync_in_worker_thread`.
56+
:meth:`trio.run_sync_in_thread`.
5757
5858
All properties and methods defined in in :mod:`~io` are exposed by this
5959
wrapper, if they exist in the wrapped file object.
@@ -80,7 +80,7 @@ def __getattr__(self, name):
8080
@async_wraps(self.__class__, self._wrapped.__class__, name)
8181
async def wrapper(*args, **kwargs):
8282
func = partial(meth, *args, **kwargs)
83-
return await trio.run_sync_in_worker_thread(func)
83+
return await trio.run_sync_in_thread(func)
8484

8585
# cache the generated method
8686
setattr(self, name, wrapper)
@@ -115,7 +115,7 @@ async def detach(self):
115115
116116
"""
117117

118-
raw = await trio.run_sync_in_worker_thread(self._wrapped.detach)
118+
raw = await trio.run_sync_in_thread(self._wrapped.detach)
119119
return wrap_file(raw)
120120

121121
async def aclose(self):
@@ -128,7 +128,7 @@ async def aclose(self):
128128

129129
# ensure the underling file is closed during cancellation
130130
with trio.CancelScope(shield=True):
131-
await trio.run_sync_in_worker_thread(self._wrapped.close)
131+
await trio.run_sync_in_thread(self._wrapped.close)
132132

133133
await trio.hazmat.checkpoint_if_cancelled()
134134

@@ -165,7 +165,7 @@ async def open_file(
165165
file = fspath(file)
166166

167167
_file = wrap_file(
168-
await trio.run_sync_in_worker_thread(
168+
await trio.run_sync_in_thread(
169169
io.open, file, mode, buffering, encoding, errors, newline, closefd,
170170
opener
171171
)

trio/_path.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def iter_wrapper_factory(cls, meth_name):
5858
async def wrapper(self, *args, **kwargs):
5959
meth = getattr(self._wrapped, meth_name)
6060
func = partial(meth, *args, **kwargs)
61-
items = await trio.run_sync_in_worker_thread(func)
61+
items = await trio.run_sync_in_thread(func)
6262
return (rewrap_path(item) for item in items)
6363

6464
return wrapper
@@ -70,7 +70,7 @@ async def wrapper(self, *args, **kwargs):
7070
args = unwrap_paths(args)
7171
meth = getattr(self._wrapped, meth_name)
7272
func = partial(meth, *args, **kwargs)
73-
value = await trio.run_sync_in_worker_thread(func)
73+
value = await trio.run_sync_in_thread(func)
7474
return rewrap_path(value)
7575

7676
return wrapper
@@ -83,7 +83,7 @@ async def wrapper(cls, *args, **kwargs):
8383
args = unwrap_paths(args)
8484
meth = getattr(cls._wraps, meth_name)
8585
func = partial(meth, *args, **kwargs)
86-
value = await trio.run_sync_in_worker_thread(func)
86+
value = await trio.run_sync_in_thread(func)
8787
return rewrap_path(value)
8888

8989
return wrapper
@@ -145,7 +145,7 @@ def generate_iter(cls, attrs):
145145

146146
class Path(metaclass=AsyncAutoWrapperType):
147147
"""A :class:`pathlib.Path` wrapper that executes blocking methods in
148-
:meth:`trio.run_sync_in_worker_thread`.
148+
:meth:`trio.run_sync_in_thread`.
149149
150150
"""
151151

@@ -185,7 +185,7 @@ async def open(self, *args, **kwargs):
185185
"""
186186

187187
func = partial(self._wrapped.open, *args, **kwargs)
188-
value = await trio.run_sync_in_worker_thread(func)
188+
value = await trio.run_sync_in_thread(func)
189189
return trio.wrap_file(value)
190190

191191

trio/_socket.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import idna as _idna
77

88
import trio
9-
from ._threads import run_sync_in_worker_thread
9+
from ._threads import run_sync_in_thread
1010
from ._util import fspath
1111
from ._core import RunVar, wait_socket_readable, wait_socket_writable
1212

@@ -178,7 +178,7 @@ def numeric_only_failure(exc):
178178
if hr is not None:
179179
return await hr.getaddrinfo(host, port, family, type, proto, flags)
180180
else:
181-
return await run_sync_in_worker_thread(
181+
return await run_sync_in_thread(
182182
_stdlib_socket.getaddrinfo,
183183
host,
184184
port,
@@ -204,7 +204,7 @@ async def getnameinfo(sockaddr, flags):
204204
if hr is not None:
205205
return await hr.getnameinfo(sockaddr, flags)
206206
else:
207-
return await run_sync_in_worker_thread(
207+
return await run_sync_in_thread(
208208
_stdlib_socket.getnameinfo, sockaddr, flags, cancellable=True
209209
)
210210

@@ -215,7 +215,7 @@ async def getprotobyname(name):
215215
Like :func:`socket.getprotobyname`, but async.
216216
217217
"""
218-
return await run_sync_in_worker_thread(
218+
return await run_sync_in_thread(
219219
_stdlib_socket.getprotobyname, name, cancellable=True
220220
)
221221

@@ -463,7 +463,7 @@ async def bind(self, address):
463463
):
464464
# Use a thread for the filesystem traversal (unless it's an
465465
# abstract domain socket)
466-
return await run_sync_in_worker_thread(self._sock.bind, address)
466+
return await run_sync_in_thread(self._sock.bind, address)
467467
else:
468468
# POSIX actually says that bind can return EWOULDBLOCK and
469469
# complete asynchronously, like connect. But in practice AFAICT

trio/_subprocess_platform/waitid.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from .. import _core, _subprocess
77
from .._sync import CapacityLimiter, Event
8-
from .._threads import run_sync_in_worker_thread
8+
from .._threads import run_sync_in_thread
99

1010
try:
1111
from os import waitid
@@ -74,7 +74,7 @@ async def _waitid_system_task(pid: int, event: Event) -> None:
7474
# call to trio.run is shutting down.
7575

7676
try:
77-
await run_sync_in_worker_thread(
77+
await run_sync_in_thread(
7878
sync_wait_reapable,
7979
pid,
8080
cancellable=True,

trio/_sync.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ class CapacityLimiter:
143143
fixed number of seats, and if they're all taken then you have to wait for
144144
someone to get up before you can sit down.
145145
146-
By default, :func:`run_sync_in_worker_thread` uses a
146+
By default, :func:`run_sync_in_thread` uses a
147147
:class:`CapacityLimiter` to limit the number of threads running at once;
148-
see :func:`current_default_worker_thread_limiter` for details.
148+
see :func:`current_default_thread_limiter` for details.
149149
150150
If you're familiar with semaphores, then you can think of this as a
151151
restricted semaphore that's specialized for one common use case, with
@@ -246,7 +246,7 @@ def acquire_on_behalf_of_nowait(self, borrower):
246246
Args:
247247
borrower: A :class:`trio.hazmat.Task` or arbitrary opaque object
248248
used to record who is borrowing this token. This is used by
249-
:func:`run_sync_in_worker_thread` to allow threads to "hold
249+
:func:`run_sync_in_thread` to allow threads to "hold
250250
tokens", with the intention in the future of using it to `allow
251251
deadlock detection and other useful things
252252
<https://github.com/python-trio/trio/issues/182>`__

0 commit comments

Comments
 (0)