Is there a more efficient threading lock?

Skip Montanaro skip.montanaro at gmail.com
Sat Feb 25 10:52:15 EST 2023


I have a multi-threaded program which calls out to a non-thread-safe
library (not mine) in a couple places. I guard against multiple
threads executing code there using threading.Lock. The code is
straightforward:

from threading import Lock

# Something in textblob and/or nltk doesn't play nice with no-gil, so just
# serialize all blobby accesses.
BLOB_LOCK = Lock()

def get_terms(text):
    with BLOB_LOCK:
        phrases = TextBlob(text, np_extractor=EXTRACTOR).noun_phrases
    for phrase in phrases:
        yield phrase

When I monitor the application using py-spy, that with statement is
consuming huge amounts of CPU. Does threading.Lock.acquire() sleep
anywhere? I didn't see anything obvious poking around in the C code
which implements this stuff. I'm no expert though, so could easily
have missed something.

Thx,

Skip


More information about the Python-list mailing list