Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve external sessions #9

Merged
merged 1 commit into from
Nov 17, 2023
Merged

Preserve external sessions #9

merged 1 commit into from
Nov 17, 2023

Conversation

DavidStirling
Copy link
Member

Scenario: User creates a connector object with connector = omero2pandas.connect_to_omero() or the standard API and passes that into functions like read_table(omero_connector=connector). The wrapper picks up the existing connection (which can be an omero2pandas object or OMERO client) and uses it to spawn a new OMEROConnection context. That context is temporary and gets deleted when reading finishes. This deletion triggers the session shutdown process, but this shouldn't happen if the user supplied their own client.

In essence, the user-provided client object's session gets closed after using it with read_table or another function. Trying to use it again will fail.

To fix this we keep a note of whether the user provided a client on connector creation, and don't shut down if this is the case.

Copy link
Member

@sbesson sbesson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I came across exactly this scenario while testing #10 as I tried

import pandas
import omero2pandas
connector = omero2pandas.connect_to_omero(server=hostname)
my_data = pandas.read_csv("test.csv")
omero2pandas.upload_table(my_data, "test table", <id>, "Project", omero_connector=connector)
omero2pandas.upload_table(my_data, "test table", <id>, "Screen", omero_connector=connector)

and the second call to upload_table failed with

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/sbesson/Documents/GitHub/omero2pandas/omero2pandas/__init__.py", line 194, in upload_table
    conn = connector.get_gateway()
  File "/Users/sbesson/Documents/GitHub/omero2pandas/omero2pandas/connect.py", line 261, in get_gateway
    self.gateway = BlitzGateway(client_obj=self.client)
  File "/Users/sbesson/Documents/GitHub/omero2pandas/venv/lib/python3.10/site-packages/omero/gateway/__init__.py", line 1600, in __init__
    self.SERVICE_OPTS = self.createServiceOptsDict()
  File "/Users/sbesson/Documents/GitHub/omero2pandas/venv/lib/python3.10/site-packages/omero/gateway/__init__.py", line 1676, in createServiceOptsDict
    serviceOpts = ServiceOptsDict(self.c.getImplicitContext().getContext())
  File "/Users/sbesson/Documents/GitHub/omero2pandas/venv/lib/python3.10/site-packages/omero/clients.py", line 522, in getImplicitContext
    return self.getCommunicator().getImplicitContext()
  File "/Users/sbesson/Documents/GitHub/omero2pandas/venv/lib/python3.10/site-packages/omero/clients.py", line 459, in getCommunicator
    raise omero.ClientError(
omero.ClientError: No Ice.Communicator active; call createSession() or create a new client instance

With this PR included the workflow described above is successful and I can make read/write calls without having to log in every time.

Unrelated to this PR, assuming I want to terminate a session created via omero2pandas.connect_to_omero, is there a standard API to close my session?

@DavidStirling
Copy link
Member Author

Unrelated to this PR, assuming I want to terminate a session created via omero2pandas.connect_to_omero, is there a standard API to close my session?

You can call connector.shutdown() or simply delete the connector object (which triggers that method automatically). When working with the connector it also forwards method lookups to the client object, so calling connector.closeSession() is equivalent to client.closeSession(), allowing people to use the more conventional API if desired.

@emilroz emilroz merged commit aba75fb into main Nov 17, 2023
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants