sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[Feature Request] Worker instance lifespan

Open funkindy opened this issue 1 year ago • 3 comments

It would be nice to have some kind of lifespan for worker instances.

In my case i need shared http client (httpx.AsyncClient) for my activities, and i now don't see a proper place to call await client.aclose().

funkindy avatar Oct 10 '24 17:10 funkindy

Can you clarify the request a bit? A worker is running and using what it has until run returns (or if you're using async with, until that block is done). A worker's run does not return until it is shutdown and all activities are done (see https://github.com/temporalio/sdk-python?tab=readme-ov-file#worker-shutdown). You should add your client close after the worker run is complete.

cretz avatar Oct 10 '24 18:10 cretz

async with has its own complex cleanup logic, which i have to reproduce in my scenario without context manager.

Why not to include the implementation of __aexit__ to some kind of asynd def cleanup(...)? Or maybe worker.shutdown is sufficient?

funkindy avatar Oct 14 '24 08:10 funkindy

Why not to include the implementation of aexit to some kind of asynd def cleanup(...)? Or maybe worker.shutdown is sufficient?

Worker shutdown should be sufficient. If you add your cleanup code after worker is shutdown (or after context manager is complete), that is enough to guarantee the worker isn't using things anymore. A worker lifetime is the life of its run or context manager, both will not complete while any activities are still running (neither will the shutdown call), add any cleanup after that as needed.

cretz avatar Oct 15 '24 14:10 cretz

Closing, but feel free to keep discussing or reopen @funkindy.

dandavison avatar Apr 29 '25 17:04 dandavison