devlib icon indicating copy to clipboard operation
devlib copied to clipboard

Use target connections as contexts.

Open setrofim opened this issue 5 years ago • 4 comments

Make it easier to use multiple connections to the target from the same thread.

setrofim avatar Jan 30 '20 07:01 setrofim

Cc @douglas-raillard-arm: this should be largely orthogonal to #450, I believe, but FYI, just in case.

setrofim avatar Jan 30 '20 07:01 setrofim

You might want to take the patches that fixes thread local variables to store the connection first, they somehow conflicts, especially on the set_connection() side that is already handled by tls_property out of the box: https://github.com/ARM-software/devlib/pull/450/commits/d8f9fac796b4293be785e89c1ff5016083d606da https://github.com/ARM-software/devlib/pull/450/commits/aa3b0e357cf27927685db9c70266ee936559dc66

I can submit them in a separate PR if needed.

There is also a ConnectionBase class introduced: https://github.com/ARM-software/devlib/pull/450/commits/d7908d10bebe741382eb1d3bdc6ab632409cedc1 But it should be straightforward to merge the features of both as they don't seem to overlap.

douglas-raillard-arm avatar Jan 30 '20 11:01 douglas-raillard-arm

There is also a ConnectionBase class introduced: d7908d1 But it should be straightforward to merge the features of both as they don't seem to overlap.

Ah cool, I'll hold off on this then, until that is merged, and will then add the changes to that. One nit -- could you rename the file to connection.py (singular) to be consistent with the other files.

setrofim avatar Jan 30 '20 12:01 setrofim

I think this PR is stale as quite a lot of things has changed since then in that area, but it would be pretty easy to implement these days with something like that:



class Target:
     ...
     @contextlib.contextmanager
     def with_connection(self, conn):
        old = self.conn
        try:
            self.conn = conn
            yield
        finally:
            self.conn = old

self.conn is defined using devlib.utils.misc.tls_property that provides a thread-local value (and creates a value when used for the first time in a given thread). It supports assignment (__set__) and that assignment will be thread-local as well. The main issue with that implementation is that it will create the old connection if it does not exist yet, but that would be easy to fix by extending the tls_property API to optionally let it raise instead of creating a new instance.

douglas-raillard-arm avatar May 08 '24 11:05 douglas-raillard-arm