Skip to content

Commit 6158d5a

Browse files
authoredJun 17, 2021
Document calling non-threadsafe primitives from threads (#314)
Also added a tox environment for buildings docs.
1 parent 247d8ad commit 6158d5a

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed
 

‎docs/contributing.rst

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ passes tests locally. To that end, the use of tox_ is recommended. The default t
99
code style fixing tools and then the actual test suite. To only run the code style fixers, run
1010
``tox -e lint``. To run the checks on all environments in parallel, invoke tox with ``tox -p``.
1111

12+
To build the documentation, run ``tox -e docs`` which will generate a directory named ``build``
13+
in which you may view the formatted HTML documentation.
14+
1215
The use of pre-commit_ is also highly recommended. You can find a sample configuration file at the
1316
root of the repository.
1417

‎docs/synchronization.rst

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ Synchronization primitives are objects that are used by tasks to communicate and
77
each other. They are useful for things like distributing workload, notifying other tasks and
88
guarding access to shared resources.
99

10+
.. note:: AnyIO primitives are not thread-safe, therefore they should not be used directly from
11+
worker threads. Use :func:`~from_thread.run_sync` for that.
12+
1013
Events
1114
------
1215

‎docs/threads.rst

+23
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,29 @@ If you need to call a coroutine function from a worker thread, you can do this::
5353
.. note:: The worker thread must have been spawned using :func:`~run_sync_in_worker_thread`
5454
for this to work.
5555

56+
Calling synchronous code from a worker thread
57+
---------------------------------------------
58+
59+
Occasionally you may need to call synchronous code in the event loop thread from a worker thread.
60+
Common cases include setting asynchronous events or sending data to a memory object stream.
61+
Because these methods aren't thread safe, you need to arrange them to be called inside the event
62+
loop thread using :func:`~from_thread.run_sync`::
63+
64+
import time
65+
66+
from anyio import Event, from_thread, to_thread, run
67+
68+
def worker(event):
69+
time.sleep(1)
70+
from_thread.run_sync(event.set)
71+
72+
async def main():
73+
event = Event()
74+
await to_thread.run_sync(worker, event)
75+
await event.wait()
76+
77+
run(main)
78+
5679
Calling asynchronous code from an external thread
5780
-------------------------------------------------
5881

‎tox.ini

+4
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,7 @@ deps = mypy >= 0.900
3636
extras = test
3737
commands = mypy {posargs} src tests
3838
skip_install = true
39+
40+
[testenv:docs]
41+
extras = doc
42+
commands = sphinx-build docs build/sphinx

0 commit comments

Comments
 (0)
Please sign in to comment.