Search code examples
pythonmultithreadinggrpc

How to realize a RW lock with multithreading gRPC?


I am trying to write a simple multi-threading server-client stock trading Python program with gRPC and concurrent.future.ThreadPoolExecutor

The server will run a specific number of threads, performs Lookup() and Trade() request sent from clients. The server will maintain a list. That means Lookup() should set read lock, and Trade() should set write lock.

However, it seems that the documentation of gRPC doesn't mention anything about RW lock. Is ThreadPoolExecutor thred-safe?

Any suggestion is appreciated!


Solution

  • A threadPoolExecutor with a max_workers greater than 1 requires that your only submit functions that reference thread-safe shared state only. For instance, you should not mutate a global variable from multiple jobs submitted to the thread pool, thus you must protect this with some kind of synchronization primitive.

    # /!\ Not thread-safe
    
    from concurrent.futures import ThreadPoolExecutor
    
    
    def mutate_value():
      global some_value
      some_value += 1
    
    
    some_value = 0
    
    with ThreadPoolExecutor(max_workers=2) as e:
      e.submit(mutate_value)
      e.submit(mutate_value)
    
    # Thread-safe
    
    from concurrent.futures import ThreadPoolExecutor
    from threading import Lock
    
    
    lock = Lock()
    
    
    def mutate_value():
      global some_value
    
      with lock:
        some_value += 1
    
    
    some_value = 0
    
    with ThreadPoolExecutor(max_workers=2) as e:
      e.submit(mutate_value)
      e.submit(mutate_value)