Skip to content

Connecting to a database#

Connecting to a Redis instance is done by creating a DataStore object with a Redis URL:

from blissdata.redis_engine.store import DataStore

data_store = DataStore("redis://localhost:6379")

Because a data store is not holding one, but a pool of sockets. There is no need to create many of them. The number of sockets in the pool automatically scales up, when parallel requests are made.

It is even recommended to reuse the same data store instance as most as possible, as it reuses previously open sockets. This prevents from opening new ones every time.

Handling scans#

Creating scans#

from blissdata.redis_engine.store import DataStore

data_store = DataStore("redis://localhost:6379")

scan_id = {
    "name": "my_scan",
    "number": 1,
    "data_policy": "None",
}
scan = data_store.create_scan(scan_id)

print(scan.key)
#> esrf:scan:01JBXVGZM0G32HDSZJXS6MSESN

print(scan.state)
#> <ScanState.CREATED: 0>

Looking for scans#

As we just saw, scans are uniquely identified within Redis. Therefore, your first step is to obtain the key for the Scan you're interested in. Here are the possible options:

  • get_next_scan(since=None, block=True, timeout=0):

    The scan doesn't exist yet and you are waiting for it.

    from blissdata.redis_engine.exceptions import NoScanAvailable
    
    # Wait 10 seconds for a new scan
    try:
        timestamp, key = data_store.get_next_scan(timeout=10)
    except NoScanAvailable:
        raise Exception("No scan started within 10 seconds")
    scan_1 = data_store.load_scan(key)
    
    # -------------- PROCESS scan_1 --------------
    
    # Wait for the immediate next scan
    timestamp, key = data_store.get_next_scan(since=timestamp)
    scan_2 = data_store.load_scan(key)
    
    # -------------- PROCESS scan_2 --------------
    
    # Get the next scan immediately or raise an Exception
    try:
        timestamp, key = data_store.get_next_scan(since=timestamp, block=False)
    except NoScanAvailable:
        raise Exception("No scan started while scan_2 processing")
    scan_3 = data_store.load_scan(key)
    

    Tip

    Reusing the previous timestamp ensure you don't miss any new scan while processing a previous one.

  • get_last_scan():

    The scan just ran.

    from blissdata.redis_engine.exceptions import NoScanAvailable
    
    try:
        timestamp, key = data_store.get_last_scan()
    except NoScanAvailable:
        raise Exception("There is no scan at all !")
    scan = data_store.load_scan(key)
    

  • search_existing_scans(**kwargs):

    The scan already exists and you want to find it among existing ones.

    timestamp, keys = data_store.search_existing_scans(session="my_session", proposal=..., ...)
    # load each scan from key, or simply uses keys to count how many scans correspond to your search...
    
    **kwargs can contain any key present in scan identity model (eg. name, number, data_policy, session, proposal, collection, dataset, path)

    Tip

    timestamp is not used there, but you can provide it to get_next_scan(since=timestamp) to make it relative to a previous research.