worker.py
import asyncio
from temporalio.client import Client, TLSConfig
from temporalio.worker import Worker
from datetime import timedelta
from temporalio import workflow
from temporalio.common import RetryPolicy
from temporalio import activity
from time import sleep
import concurrent.futures
import threading
def slp(sec):
sleep(sec)
return f"slept {sec} sec"
@activity.defn(name="Sleeping 1")
async def sleeping1():
activity.logger.info("Running activity with parameter")
response = slp(20)
return response
@activity.defn(name="sleeping 2")
async def sleeping2():
activity.logger.info("Running activity with parameter")
response = slp(120)
return response
@workflow.defn
class sleepingWF:
@workflow.run
async def run(self, body):
workflow.logger.info(f"Running workflow with parameter {body}")
s1 = await workflow.execute_activity(
sleeping1,
start_to_close_timeout=timedelta(seconds=3000),
retry_policy=RetryPolicy(
backoff_coefficient=1.0,
maximum_attempts=1,
initial_interval=timedelta(seconds=30),
maximum_interval=timedelta(seconds=30),
)
)
s1 = await workflow.execute_activity(
sleeping2,
start_to_close_timeout=timedelta(seconds=3000),
retry_policy=RetryPolicy(
backoff_coefficient=1.0,
maximum_attempts=1,
initial_interval=timedelta(seconds=30),
maximum_interval=timedelta(seconds=30),
)
)
async def main():
TEMPORAL_ENDPOINT = "localhost:7233"
TASK_QUEUE = "mymac"
client = await Client.connect(
target_host=TEMPORAL_ENDPOINT
)
with concurrent.futures.ThreadPoolExecutor(max_workers=100) as activity_executor:
worker = Worker(
client,
task_queue=TASK_QUEUE,
workflows=[sleepingWF],
activities=[sleeping1, sleeping2],
activity_executor=activity_executor,
max_concurrent_workflow_tasks=10,
max_concurrent_activities=10,
max_concurrent_workflow_task_polls=10,
max_concurrent_activity_task_polls=10,
)
await worker.run()
if __name__ == "__main__":
asyncio.run(main())
client.py
import asyncio
from temporalio.client import Client
async def main():
# Create client connected to server at the given address and namespace
client = await Client.connect("localhost:7233")
# Start a workflow
handle = await client.start_workflow("sleepingWF", "some arg", id="my-workflow-id0", task_queue="mymac")
# Wait for result
result = await handle.result()
print(f"Result: {result}")
asyncio.run(main())
client2.py
import asyncio
from temporalio.client import Client
async def main():
# Create client connected to server at the given address and namespace
client = await Client.connect("localhost:7233")
# Start a workflow
handle = await client.start_workflow("sleepingWF", "some arg", id="my-workflow-id1", task_queue="mymac")
# Wait for result
result = await handle.result()
print(f"Result: {result}")
asyncio.run(main())
output :
output json:
my-workflow-id0.json
{
"events": [
{
"eventId": "1",
"eventTime": "2024-03-18T06:56:37.578511673Z",
"eventType": "WorkflowExecutionStarted",
"version": "0",
"taskId": "3145791",
"workerMayIgnore": false,
"workflowExecutionStartedEventAttributes": {
"workflowType": {
"name": "sleepingWF"
},
"parentWorkflowNamespace": "",
"parentWorkflowNamespaceId": "",
"parentWorkflowExecution": null,
"parentInitiatedEventId": "0",
"taskQueue": {
"name": "mymac",
"kind": "Normal",
"normalName": ""
},
"input": {
"payloads": [
{
"metadata": {
"encoding": "anNvbi9wbGFpbg=="
},
"data": "InNvbWUgYXJnIg=="
}
]
},
"workflowExecutionTimeout": null,
"workflowRunTimeout": null,
"workflowTaskTimeout": "10s",
"continuedExecutionRunId": "",
"initiator": "Unspecified",
"continuedFailure": null,
"lastCompletionResult": null,
"originalExecutionRunId": "6e38a6ef-847f-49e1-a3dc-412d8a3a5c6e",
"identity": "38216@INM1JJ64R6K6T",
"firstExecutionRunId": "6e38a6ef-847f-49e1-a3dc-412d8a3a5c6e",
"retryPolicy": null,
"attempt": 1,
"workflowExecutionExpirationTime": null,
"cronSchedule": "",
"firstWorkflowTaskBackoff": "0s",
"memo": null,
"searchAttributes": null,
"prevAutoResetPoints": null,
"header": null,
"parentInitiatedEventVersion": "0",
"workflowId": "my-workflow-id0",
"sourceVersionStamp": null
}
},
{
"eventId": "2",
"eventTime": "2024-03-18T06:56:37.578565090Z",
"eventType": "WorkflowTaskScheduled",
"version": "0",
"taskId": "3145792",
"workerMayIgnore": false,
"workflowTaskScheduledEventAttributes": {
"taskQueue": {
"name": "mymac",
"kind": "Normal",
"normalName": ""
},
"startToCloseTimeout": "10s",
"attempt": 1
}
},
{
"eventId": "3",
"eventTime": "2024-03-18T06:56:37.586486381Z",
"eventType": "WorkflowTaskStarted",
"version": "0",
"taskId": "3145797",
"workerMayIgnore": false,
"workflowTaskStartedEventAttributes": {
"scheduledEventId": "2",
"identity": "37766@INM1JJ64R6K6T",
"requestId": "ec606e90-0c7d-4baa-90d1-721204981d03",
"suggestContinueAsNew": false,
"historySizeBytes": "264"
}
},
{
"eventId": "4",
"eventTime": "2024-03-18T06:56:37.597013965Z",
"eventType": "WorkflowTaskCompleted",
"version": "0",
"taskId": "3145801",
"workerMayIgnore": false,
"workflowTaskCompletedEventAttributes": {
"scheduledEventId": "2",
"startedEventId": "3",
"identity": "37766@INM1JJ64R6K6T",
"binaryChecksum": "",
"workerVersion": {
"buildId": "9c3a7fcf3b9921bdcbff57f9e5f0d252",
"bundleId": "",
"useVersioning": false
},
"sdkMetadata": {
"coreUsedFlags": [
2,
1
],
"langUsedFlags": []
},
"meteringMetadata": {
"nonfirstLocalActivityExecutionAttempts": 0
}
}
},
{
"eventId": "5",
"eventTime": "2024-03-18T06:56:37.597051340Z",
"eventType": "ActivityTaskScheduled",
"version": "0",
"taskId": "3145802",
"workerMayIgnore": false,
"activityTaskScheduledEventAttributes": {
"activityId": "1",
"activityType": {
"name": "Sleeping 1"
},
"taskQueue": {
"name": "mymac",
"kind": "Normal",
"normalName": ""
},
"header": {
"fields": {}
},
"input": null,
"scheduleToCloseTimeout": "0s",
"scheduleToStartTimeout": "0s",
"startToCloseTimeout": "3000s",
"heartbeatTimeout": "0s",
"workflowTaskCompletedEventId": "4",
"retryPolicy": {
"initialInterval": "30s",
"backoffCoefficient": 1,
"maximumInterval": "30s",
"maximumAttempts": 1,
"nonRetryableErrorTypes": []
},
"useCompatibleVersion": true
}
},
{
"eventId": "6",
"eventTime": "2024-03-18T06:56:37.599746840Z",
"eventType": "ActivityTaskStarted",
"version": "0",
"taskId": "3145808",
"workerMayIgnore": false,
"activityTaskStartedEventAttributes": {
"scheduledEventId": "5",
"identity": "37766@INM1JJ64R6K6T",
"requestId": "adc44575-75c4-426c-8dc7-01081fa89581",
"attempt": 1,
"lastFailure": null
}
},
{
"eventId": "7",
"eventTime": "2024-03-18T06:56:57.611806168Z",
"eventType": "ActivityTaskCompleted",
"version": "0",
"taskId": "3145809",
"workerMayIgnore": false,
"activityTaskCompletedEventAttributes": {
"result": {
"payloads": [
{
"metadata": {
"encoding": "anNvbi9wbGFpbg=="
},
"data": "InNsZXB0IDIwIHNlYyI="
}
]
},
"scheduledEventId": "5",
"startedEventId": "6",
"identity": "37766@INM1JJ64R6K6T",
"workerVersion": null
}
},
{
"eventId": "8",
"eventTime": "2024-03-18T06:56:57.611833835Z",
"eventType": "WorkflowTaskScheduled",
"version": "0",
"taskId": "3145810",
"workerMayIgnore": false,
"workflowTaskScheduledEventAttributes": {
"taskQueue": {
"name": "37766@INM1JJ64R6K6T-7ea82879f0c247088111c613cd74dc0c",
"kind": "Sticky",
"normalName": "mymac"
},
"startToCloseTimeout": "10s",
"attempt": 1
}
},
{
"eventId": "9",
"eventTime": "2024-03-18T06:56:57.626587043Z",
"eventType": "WorkflowTaskStarted",
"version": "0",
"taskId": "3145814",
"workerMayIgnore": false,
"workflowTaskStartedEventAttributes": {
"scheduledEventId": "8",
"identity": "37766@INM1JJ64R6K6T",
"requestId": "1ecfd1f8-a53a-487b-af95-d2f03daa4a4c",
"suggestContinueAsNew": false,
"historySizeBytes": "830"
}
},
{
"eventId": "10",
"eventTime": "2024-03-18T06:56:57.637189710Z",
"eventType": "WorkflowTaskCompleted",
"version": "0",
"taskId": "3145818",
"workerMayIgnore": false,
"workflowTaskCompletedEventAttributes": {
"scheduledEventId": "8",
"startedEventId": "9",
"identity": "37766@INM1JJ64R6K6T",
"binaryChecksum": "",
"workerVersion": {
"buildId": "9c3a7fcf3b9921bdcbff57f9e5f0d252",
"bundleId": "",
"useVersioning": false
},
"sdkMetadata": {
"coreUsedFlags": [],
"langUsedFlags": []
},
"meteringMetadata": {
"nonfirstLocalActivityExecutionAttempts": 0
}
}
},
{
"eventId": "11",
"eventTime": "2024-03-18T06:56:57.637235043Z",
"eventType": "ActivityTaskScheduled",
"version": "0",
"taskId": "3145819",
"workerMayIgnore": false,
"activityTaskScheduledEventAttributes": {
"activityId": "2",
"activityType": {
"name": "sleeping 2"
},
"taskQueue": {
"name": "mymac",
"kind": "Normal",
"normalName": ""
},
"header": {
"fields": {}
},
"input": null,
"scheduleToCloseTimeout": "0s",
"scheduleToStartTimeout": "0s",
"startToCloseTimeout": "3000s",
"heartbeatTimeout": "0s",
"workflowTaskCompletedEventId": "10",
"retryPolicy": {
"initialInterval": "30s",
"backoffCoefficient": 1,
"maximumInterval": "30s",
"maximumAttempts": 1,
"nonRetryableErrorTypes": []
},
"useCompatibleVersion": true
}
},
{
"eventId": "12",
"eventTime": "2024-03-18T06:56:57.642842460Z",
"eventType": "ActivityTaskStarted",
"version": "0",
"taskId": "3145824",
"workerMayIgnore": false,
"activityTaskStartedEventAttributes": {
"scheduledEventId": "11",
"identity": "37766@INM1JJ64R6K6T",
"requestId": "6edc3a0b-d0d7-469a-bd4b-a67c4ff19cda",
"attempt": 1,
"lastFailure": null
}
},
{
"eventId": "13",
"eventTime": "2024-03-18T06:58:57.657770418Z",
"eventType": "ActivityTaskCompleted",
"version": "0",
"taskId": "3145825",
"workerMayIgnore": false,
"activityTaskCompletedEventAttributes": {
"result": {
"payloads": [
{
"metadata": {
"encoding": "anNvbi9wbGFpbg=="
},
"data": "InNsZXB0IDEyMCBzZWMi"
}
]
},
"scheduledEventId": "11",
"startedEventId": "12",
"identity": "37766@INM1JJ64R6K6T",
"workerVersion": null
}
},
{
"eventId": "14",
"eventTime": "2024-03-18T06:58:57.657794543Z",
"eventType": "WorkflowTaskScheduled",
"version": "0",
"taskId": "3145826",
"workerMayIgnore": false,
"workflowTaskScheduledEventAttributes": {
"taskQueue": {
"name": "37766@INM1JJ64R6K6T-7ea82879f0c247088111c613cd74dc0c",
"kind": "Sticky",
"normalName": "mymac"
},
"startToCloseTimeout": "10s",
"attempt": 1
}
},
{
"eventId": "15",
"eventTime": "2024-03-18T06:58:57.675632377Z",
"eventType": "WorkflowTaskStarted",
"version": "0",
"taskId": "3145830",
"workerMayIgnore": false,
"workflowTaskStartedEventAttributes": {
"scheduledEventId": "14",
"identity": "37766@INM1JJ64R6K6T",
"requestId": "9567d344-8573-46e1-9323-45a4165c2bb0",
"suggestContinueAsNew": false,
"historySizeBytes": "1393"
}
},
{
"eventId": "16",
"eventTime": "2024-03-18T06:59:07.676871090Z",
"eventType": "WorkflowTaskTimedOut",
"version": "0",
"taskId": "3145834",
"workerMayIgnore": false,
"workflowTaskTimedOutEventAttributes": {
"scheduledEventId": "14",
"startedEventId": "15",
"timeoutType": "StartToClose"
}
},
{
"eventId": "17",
"eventTime": "2024-03-18T06:59:07.676875798Z",
"eventType": "WorkflowTaskScheduled",
"version": "0",
"taskId": "3145835",
"workerMayIgnore": false,
"workflowTaskScheduledEventAttributes": {
"taskQueue": {
"name": "mymac",
"kind": "Normal",
"normalName": ""
},
"startToCloseTimeout": "10s",
"attempt": 1
}
},
{
"eventId": "18",
"eventTime": "2024-03-18T06:59:07.680358631Z",
"eventType": "WorkflowTaskStarted",
"version": "0",
"taskId": "3145838",
"workerMayIgnore": false,
"workflowTaskStartedEventAttributes": {
"scheduledEventId": "17",
"identity": "37766@INM1JJ64R6K6T",
"requestId": "5f5fb8e9-4f60-4bc2-8ef0-eb61af93f2f1",
"suggestContinueAsNew": false,
"historySizeBytes": "1561"
}
},
{
"eventId": "19",
"eventTime": "2024-03-18T06:59:17.690417470Z",
"eventType": "WorkflowTaskTimedOut",
"version": "0",
"taskId": "3145842",
"workerMayIgnore": false,
"workflowTaskTimedOutEventAttributes": {
"scheduledEventId": "17",
"startedEventId": "18",
"timeoutType": "StartToClose"
}
},
{
"eventId": "20",
"eventTime": "2024-03-18T06:59:17.690424636Z",
"eventType": "WorkflowTaskScheduled",
"version": "0",
"taskId": "3145847",
"workerMayIgnore": false,
"workflowTaskScheduledEventAttributes": {
"taskQueue": {
"name": "mymac",
"kind": "Normal",
"normalName": ""
},
"startToCloseTimeout": "10s",
"attempt": 2
}
},
{
"eventId": "21",
"eventTime": "2024-03-18T06:59:17.695203303Z",
"eventType": "WorkflowTaskStarted",
"version": "0",
"taskId": "3145848",
"workerMayIgnore": false,
"workflowTaskStartedEventAttributes": {
"scheduledEventId": "20",
"identity": "37766@INM1JJ64R6K6T",
"requestId": "1c81b990-7c88-4142-acab-50e19dfb3f11",
"suggestContinueAsNew": false,
"historySizeBytes": "1685"
}
},
{
"eventId": "22",
"eventTime": "2024-03-18T06:59:17.714181011Z",
"eventType": "WorkflowTaskCompleted",
"version": "0",
"taskId": "3145849",
"workerMayIgnore": false,
"workflowTaskCompletedEventAttributes": {
"scheduledEventId": "20",
"startedEventId": "21",
"identity": "37766@INM1JJ64R6K6T",
"binaryChecksum": "",
"workerVersion": {
"buildId": "9c3a7fcf3b9921bdcbff57f9e5f0d252",
"bundleId": "",
"useVersioning": false
},
"sdkMetadata": {
"coreUsedFlags": [],
"langUsedFlags": []
},
"meteringMetadata": {
"nonfirstLocalActivityExecutionAttempts": 0
}
}
},
{
"eventId": "23",
"eventTime": "2024-03-18T06:59:17.714199261Z",
"eventType": "WorkflowExecutionCompleted",
"version": "0",
"taskId": "3145850",
"workerMayIgnore": false,
"workflowExecutionCompletedEventAttributes": {
"result": {
"payloads": [
{
"metadata": {
"encoding": "YmluYXJ5L251bGw="
},
"data": null
}
]
},
"workflowTaskCompletedEventId": "22",
"newExecutionRunId": ""
}
}
]
}
my-workflow-id1.json
{
"eventId": "1",
"eventTime": "2024-03-18T06:56:39.269406632Z",
"eventType": "WorkflowExecutionStarted",
"version": "0",
"taskId": "3145732",
"workerMayIgnore": false,
"workflowExecutionStartedEventAttributes": {
"workflowType": {
"name": "sleepingWF"
},
"parentWorkflowNamespace": "",
"parentWorkflowNamespaceId": "",
"parentWorkflowExecution": null,
"parentInitiatedEventId": "0",
"taskQueue": {
"name": "mymac",
"kind": "Normal",
"normalName": ""
},
"input": {
"payloads": [
"some arg"
]
},
"workflowExecutionTimeout": null,
"workflowRunTimeout": null,
"workflowTaskTimeout": "10s",
"continuedExecutionRunId": "",
"initiator": "Unspecified",
"continuedFailure": null,
"lastCompletionResult": null,
"originalExecutionRunId": "38fd291b-abc4-46eb-af48-af3a4a43bda1",
"identity": "38223@INM1JJ64R6K6T",
"firstExecutionRunId": "38fd291b-abc4-46eb-af48-af3a4a43bda1",
"retryPolicy": null,
"attempt": 1,
"workflowExecutionExpirationTime": null,
"cronSchedule": "",
"firstWorkflowTaskBackoff": "0s",
"memo": null,
"searchAttributes": null,
"prevAutoResetPoints": null,
"header": null,
"parentInitiatedEventVersion": "0",
"workflowId": "my-workflow-id1",
"sourceVersionStamp": null
},
"name": "WorkflowExecutionStarted",
"id": "1",
"timestamp": "2024-03-18 UTC 06:56:39.26",
"classification": "Started",
"category": "workflow",
"attributes": {
"type": "workflowExecutionStartedEventAttributes",
"workflowType": "sleepingWF",
"parentWorkflowNamespace": "",
"parentWorkflowNamespaceId": "",
"parentWorkflowExecution": null,
"parentInitiatedEventId": "0",
"taskQueue": {
"name": "mymac",
"kind": "Normal",
"normalName": ""
},
"input": {
"payloads": [
"some arg"
]
},
"workflowExecutionTimeout": "",
"workflowRunTimeout": "",
"workflowTaskTimeout": "10 seconds",
"continuedExecutionRunId": "",
"initiator": "Unspecified",
"continuedFailure": null,
"lastCompletionResult": null,
"originalExecutionRunId": "38fd291b-abc4-46eb-af48-af3a4a43bda1",
"identity": "38223@INM1JJ64R6K6T",
"firstExecutionRunId": "38fd291b-abc4-46eb-af48-af3a4a43bda1",
"retryPolicy": null,
"attempt": 1,
"workflowExecutionExpirationTime": "",
"cronSchedule": "",
"firstWorkflowTaskBackoff": "",
"memo": null,
"searchAttributes": null,
"prevAutoResetPoints": null,
"header": null,
"parentInitiatedEventVersion": "0",
"workflowId": "my-workflow-id1",
"sourceVersionStamp": null
}
}