To improve execution time when calling repeated-capability methods over gRPC in Python, you can use worker threads to execute the calls concurrently instead of iterating over each call sequentially.
One example of this is the 'fetch_history_ram_cycle_information' method in the nidigital Python module. Ordinarily when fetching multiple records, the method iterates over each record one at a time in sequence by calling self._fetch_sample for each record) and then waiting to start record n+1 until record n is fully fetched.
To work around this, you can use ThreadPoolExecutor to have each record run on its own worker thread. For example:
sample_indexes = range(position, position + samples_to_read)
with ThreadPoolExecutor(max_workers=self._max_workers) as executor:
# map preserves input order, so records stay in sample order.
samples = list(
executor.map(
lambda index: self._fetch_sample(site, pin_list, index),
sample_indexes,
)
)
This is just one example; this could potentially work in other situations where calls are truly independent and latency becomes a concern when executing sequential calls.