pythonmultithreadingpython-trio

Python: ways to synchronize trio tasks and regular threads


I find myself in the situation that I need to synchronize Trio tasks with Python threads. At the moment, I am using threading.Lock objects that Trio tasks have to acquire with trio.run_sync_in_worker_thread(lock.acquire).

I think it should also be possible to use trio. Lock locks and have the threads acquire them with trio.BlockingTrioPortal.run_sync(lock.acquire).

Does either of these solutions have advantages over the other?

Would in in principle be possible to do better than this? E.g. to implement a "native" trio method that waits for a threading.Lock without the need for a separate worker thread, or are there fundamental reasons why this is required?


Solution

  • Both methods are fine. I'd recommend to use the method which causes less work, i.e. if the external thread acquires the lock ten times per Trio task's once, then use a thread lock, and vice versa.

    A threading.Lock always blocks the thread trying to acquire it, so taking it from Trio either requires a separate thread (which your first method already does anyway), or the lock's holder needs to signal the Trio task that it has released the lock (which your second method does anyway), so there is no clear advantage for implementing a lower-level solution.