You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/develop/python/python-sdk-sync-vs-async.mdx
+13-9Lines changed: 13 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -84,8 +84,13 @@ Python workers have following components for executing code:
84
84
85
85
### Workflows
86
86
87
-
Since Workflow Tasks (1) are CPU bound, (2) need to be timed out for deadlock detection, and (3) need to not block other Workflow Tasks, they are run in threads. The `workflow_task_executor` is the thread pool these Tasks are run on.
87
+
Since Workflow Tasks have the following three properties, they're run in threads.
88
88
89
+
* are CPU bound
90
+
* need to be timed out for deadlock detection
91
+
* need to not block other Workflow Tasks
92
+
93
+
The `workflow_task_executor` is the thread pool these Tasks are run on.
89
94
The fact that Workflow Tasks run in a thread pool can be confusing at first because Workflow Definitions are `async`.
90
95
The key differentiator is that the `async` in Workflow Definitions isn't referring to the standard event loop -- it's referring to the Workflow's own event loop.
91
96
Each Workflow gets its own “Workflow event loop,” which is deterministic, and described in [the Python SDK blog](https://temporal.io/blog/durable-distributed-asyncio-event-loop#temporal-workflows-are-asyncio-event-loops).
@@ -94,21 +99,21 @@ When it can no longer make progress on any of its futures, then the Workflow Tas
94
99
95
100
### Number of CPU cores
96
101
97
-
The only ways to use more than one core in a python worker (considering Python's GIL) are:
102
+
The only ways to use more than one core in a python Worker (considering Python's GIL) are:
98
103
99
-
- Run more than one worker process.
104
+
- Run more than one Worker Process.
100
105
- Run the synchronous Activities in a process pool executor, but a thread pool executor is recommended.
101
106
102
-
### Separating Activity and Workflow Workers
107
+
### A Worker infrastructure option: Separate Activity and Workflow Workers
103
108
104
109
To reduce the risk of event loops or executors getting blocked,
105
110
some users choose to deploy separate Workers for Workflow Tasks and Activity Tasks.
106
111
107
112
## Activity Definition
108
113
109
114
**By default, Activities should be synchronous rather than asynchronous**.
110
-
You should only make an Activity asynchronous if you are_very_
111
-
sure that it does not block the event loop.
115
+
You should only make an Activity asynchronous if you are
116
+
certain that it doesn't block the event loop.
112
117
113
118
This is because if you have blocking code in an `async def` function,
114
119
it blocks your event loop and the rest of Temporal, which can cause bugs that are
@@ -123,11 +128,10 @@ which helps because:
123
128
- If you have multiple
124
129
Activity Tasks running in a thread pool rather than an event loop, one bad
125
130
Activity Task can't slow down the others; this is because the OS scheduler preemptively
126
-
switches between threads, which the event loop coordinator does not do.
131
+
switches between threads, which the event loop coordinator doesn't do.
127
132
128
133
> See Also:
129
134
> ["Types of Activities" section of Python SDK README](https://github.com/temporalio/sdk-python#types-of-activities)
130
-
> and [Sync vs Async activity implementations](https://docs.temporal.io/develop/python/python-sdk-sync-vs-async)
131
135
132
136
## How to implement Synchronous Activities
133
137
@@ -162,7 +166,7 @@ class TranslateActivities:
162
166
163
167
The preceeding example doesn't share a session across the Activity, so
164
168
`__init__` was removed. While `requests` does have the ability to create sessions,
165
-
it's currently unknown if they are thread safe. Due to no longer having or needing
169
+
it's currently unknown if they're thread safe. Due to no longer having or needing
166
170
`__init__`, the case could be made here to not implement the Activities as a class,
0 commit comments