Skip to content

Commit 906456b

Browse files
authored
Merge pull request #1122 from epellis/master
Deprecate BlockingTrioPortal in favor of from_thread.run and from_thread.run_sync
2 parents 5bcfb1b + 77bbdac commit 906456b

File tree

9 files changed

+320
-157
lines changed

9 files changed

+320
-157
lines changed

docs/source/history.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ Upcoming breaking changes with warnings (i.e., stuff that in 0.2.0
564564

565565
* We took the opportunity to refactor ``run_in_trio_thread`` and
566566
``await_in_trio_thread`` into the new class
567-
:class:`trio.BlockingTrioPortal`
567+
``trio.BlockingTrioPortal``
568568

569569
* The hazmat function ``current_call_soon_thread_and_signal_safe``
570570
is being replaced by :class:`trio.hazmat.TrioToken`

docs/source/reference-core.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,8 +1472,8 @@ for working with real, operating-system level,
14721472
:mod:`threading`\-module-style threads. First, if you're in Trio but
14731473
need to push some blocking I/O into a thread, there's
14741474
:func:`run_sync_in_thread`. And if you're in a thread and need
1475-
to communicate back with Trio, you can use a
1476-
:class:`BlockingTrioPortal`.
1475+
to communicate back with Trio, you can use
1476+
:func:`trio.from_thread.run` and :func:`trio.from_thread.run_sync`.
14771477

14781478

14791479
.. _worker-thread-limiting:
@@ -1611,14 +1611,15 @@ Putting blocking I/O into worker threads
16111611
Getting back into the Trio thread from another thread
16121612
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16131613

1614-
.. autoclass:: BlockingTrioPortal
1615-
:members:
1614+
.. autofunction:: trio.from_thread.run
1615+
1616+
.. autofunction:: trio.from_thread.run_sync
16161617

16171618
This will probably be clearer with an example. Here we demonstrate how
16181619
to spawn a child thread, and then use a :ref:`memory channel
16191620
<channels>` to send messages between the thread and a Trio task:
16201621

1621-
.. literalinclude:: reference-core/blocking-trio-portal-example.py
1622+
.. literalinclude:: reference-core/from-thread-example.py
16221623

16231624

16241625
Exceptions and warnings

docs/source/reference-core/blocking-trio-portal-example.py renamed to docs/source/reference-core/from-thread-example.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
import trio
22

3-
def thread_fn(portal, receive_from_trio, send_to_trio):
3+
4+
def thread_fn(receive_from_trio, send_to_trio):
45
while True:
56
# Since we're in a thread, we can't call methods on Trio
6-
# objects directly -- so we use our portal to call them.
7+
# objects directly -- so we use trio.from_thread to call them.
78
try:
8-
request = portal.run(receive_from_trio.receive)
9+
request = trio.from_thread.run(receive_from_trio.receive)
910
except trio.EndOfChannel:
10-
portal.run(send_to_trio.aclose)
11+
trio.from_thread.run(send_to_trio.aclose)
1112
return
1213
else:
1314
response = request + 1
14-
portal.run(send_to_trio.send, response)
15+
trio.from_thread.run(send_to_trio.send, response)
16+
1517

1618
async def main():
17-
portal = trio.BlockingTrioPortal()
1819
send_to_thread, receive_from_trio = trio.open_memory_channel(0)
1920
send_to_trio, receive_from_thread = trio.open_memory_channel(0)
2021

2122
async with trio.open_nursery() as nursery:
2223
# In a background thread, run:
2324
# thread_fn(portal, receive_from_trio, send_to_trio)
2425
nursery.start_soon(
25-
trio.run_sync_in_thread,
26-
thread_fn, portal, receive_from_trio, send_to_trio
26+
trio.run_sync_in_thread, thread_fn, receive_from_trio, send_to_trio
2727
)
2828

2929
# prints "1"
@@ -40,4 +40,5 @@ async def main():
4040
# When we exit the nursery, it waits for the background thread to
4141
# exit.
4242

43+
4344
trio.run(main)

docs/source/reference-hazmat.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,11 @@ These transitions are accomplished using two function decorators:
280280
function).
281281

282282
An example of where you'd use this is in implementing something
283-
like :meth:`trio.BlockingTrioPortal.run`, which uses
283+
like :func:`trio.from_thread.run`, which uses
284284
:meth:`TrioToken.run_sync_soon` to get into the Trio
285285
thread. :meth:`~TrioToken.run_sync_soon` callbacks are run with
286286
:exc:`KeyboardInterrupt` protection enabled, and
287-
:meth:`~trio.BlockingTrioPortal.run` takes advantage of this to safely set up
287+
:func:`trio.from_thread.run` takes advantage of this to safely set up
288288
the machinery for sending a response back to the original thread, but
289289
then uses :func:`disable_ki_protection` when entering the
290290
user-provided function.

trio/__init__.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@
3232
Event, CapacityLimiter, Semaphore, Lock, StrictFIFOLock, Condition
3333
)
3434

35-
from ._threads import (
36-
run_sync_in_thread, current_default_thread_limiter, BlockingTrioPortal
37-
)
35+
from ._threads import (run_sync_in_thread, current_default_thread_limiter)
36+
from ._threads import BlockingTrioPortal as _BlockingTrioPortal
3837

3938
from ._highlevel_generic import aclose_forcefully, StapledStream
4039

@@ -72,6 +71,7 @@
7271
from . import hazmat
7372
from . import socket
7473
from . import abc
74+
from . import from_thread
7575
# Not imported by default: testing
7676
if False:
7777
from . import testing
@@ -142,6 +142,13 @@
142142
"0.12.0",
143143
issue=878,
144144
),
145+
"BlockingTrioPortal":
146+
_deprecate.DeprecatedAttribute(
147+
_BlockingTrioPortal,
148+
"0.12.0",
149+
issue=810,
150+
instead=from_thread,
151+
),
145152
}
146153

147154
# Having the public path in .__module__ attributes is important for:
@@ -155,6 +162,7 @@
155162
fixup_module_metadata(hazmat.__name__, hazmat.__dict__)
156163
fixup_module_metadata(socket.__name__, socket.__dict__)
157164
fixup_module_metadata(abc.__name__, abc.__dict__)
165+
fixup_module_metadata(from_thread.__name__, from_thread.__dict__)
158166
fixup_module_metadata(__name__ + ".ssl", _deprecated_ssl_reexports.__dict__)
159167
fixup_module_metadata(
160168
__name__ + ".subprocess", _deprecated_subprocess_reexports.__dict__

trio/_core/_exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class TrioInternalError(Exception):
1818

1919

2020
class RunFinishedError(RuntimeError):
21-
"""Raised by `BlockingTrioPortal.run` and similar functions if the
21+
"""Raised by `trio.from_thread.run` and similar functions if the
2222
corresponding call to :func:`trio.run` has already finished.
2323
2424
"""

0 commit comments

Comments
 (0)