is there a way to share the same instance of a res...
# ask-community
h
is there a way to share the same instance of a resource between all my assets? I have an API client for an external service as a resource, and one set of credentials for this service. If asset
A
logs in and uses the API client, and then asset
B
logs in, asset `A`'s API session will be invalidated. Therefore I want there to be a single API client that is shared between my assets (not multiple instances of the API client). Any help would be appreciated!
👀 1
a
Also interested in topic.
t
Hi! Let me check in with the team about this. In the meantime, do you mind going into more detail about why the API sessions need to be invalidated?
Alright, back with an update! Depending on how those assets are connected, you could use the in_process_executor to keep it instantiated, with the restriction that this currently runs serially Aside from that, there isn't a great way to do it right now. We're tracking it in this issue.
h
Hi Tim, Thanks for the response. The session invalidation is due to the external third party API server. Basically when you send a login request, the server will send back a fresh session token, and invalidate any other session tokens that may be held elsewhere. Our API client is pretty dumb and just logs in on creation. This doesn't work if there are multiple overlapping API clients. One way around that is we could have one asset that logs in, returns the current session token. Then all downstream assets will depend on this session token, and then we refactor the API client to take to a session token as input. It seems like sharing instances across assets doesn't work like this. I may try to go the shared session token route to keep the parallelism.
To close the loop on this: I refactored the API client and now have two constructor methods: the original one using email+password, and a new one built from auth cookies that the external service sends us on successful login. I created an asset logs in using the email+password and returns these cookies, and then downstream assets now depend on these cookies, and construct their own API client using the new constructor I added. So instead of sharing the API client, I just share the cookies. It may be possible to declare the API client itself as an asset (assuming its pickle-able) but I didn't investigate this.