Accessing Temporal Client from within Activity

I need to query the state of a workflow from within another workflow activity. I am using the workflow.query method but am running into issues provisioning the activity with a temporal client. How can I create or access the temporal client from within the activity?

I have tried duplicating the client manager structure from this link: samples-python/hello/hello_activity_method.py at main · temporalio/samples-python · GitHub
but it either causes HTTP.client errors or isn’t able to access the initialized client from the worker.py file.

Are there any examples on how to provision an activity with the temporal client so that it can do a workflow query?

Can you show your code or explain the failures better? You can take that example and replace the DB client with a Temporal client like so:

class MyActivities:
    def __init__(self, temporal_client: temporalio.client.Client) -> None:
        self.temporal_client = temporal_client

    @activity.defn
    async def start_workflow(self) -> None:
        await self.temporal_client.start_workflow(...)

Yeah so that is what I did with the example, importing those activities into the workflow file once that was added triggered the HTTP.client passthrough error.

I can send a code snippet in a bit but what I ended up doing to resolve it for now is adding string names to the activity definition so that I don’t have to import the activities in the workflow file but can still reference them.

Ah, yes, that sample doesn’t need to passthrough imports because it doesn’t use anything non-deterministic and it’s in the same file. But for most activity imports, they need to be passed through. You can look at other samples like the langchain workflow to see how it does it.

1 Like