Skip to content

brief asyncio / multiprocessing, remove link to external asyncio guide #1286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion content/develop/concepts/app-design/multithreading.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ Multithreading is a type of concurrency, which improves the efficiency of comput

Multithreading is just one type of concurrency. Multiprocessing and coroutines are other forms of concurrency. You need to understand how your code is bottlenecked to choose the correct kind of concurrency.

Multiprocessing is inherently parallel, meaning that resources are split and multiple tasks are performed simultaneously. Therefore, multiprocessing is helpful with compute-bound operations. In contrast, multithreading and coroutines are not inherently parallel and instead allow resource switching. This makes them good choices when your code is stuck _waiting_ for something, like an IO operation. AsyncIO uses coroutines and may be preferable with very slow IO operations. Threading may be preferable with faster IO operations. For a helpful guide to using AsyncIO with Streamlit, see this [Medium article by Sehmi-Conscious Thoughts](https://sehmi-conscious.medium.com/got-that-asyncio-feeling-f1a7c37cab8b).
**Multithreading** is about more _threads_ in the current process.

**Multiprocessing** uses more _processes_ and is inherently parallel, meaning that resources are split and multiple tasks are performed simultaneously. Therefore, multiprocessing is most helpful with compute-bound operations that would otherwise be stuck at Python's [GIL](https://en.wikipedia.org/wiki/Global_interpreter_lock).

**AsyncIO** is a relatively new option to run _coroutines_ concurrently in single thread.

As multithreading and coroutines are still in current process, they are not inherently parallel and instead allow resource switching. This makes them good choices when your code is stuck _waiting_ for something, like an IO operation. AsyncIO uses coroutines and may be preferable with plenty or very slow IO operations. Threading may be preferable with faster IO operations.

Don't forget that Streamlit has [fragments](/develop/concepts/architecture/fragments) and [caching](/develop/concepts/architecture/caching), too! Use caching to avoid unnecessarily repeating computations or IO operations. Use fragments to isolate a bit of code you want to update separately from the rest of the app. You can set fragments to rerun at a specified interval, so they can be used to stream updates to a chart or table.

Expand Down