When I read the documentation, it seems the lock object is mentioned in both threading and
thread library, so I found there are 2 ways to create a lock: threading.Lock() and thread.allocate_lock(), and the return values are both of type thread.lock.
>>> a = thread.allocate_lock()
>>> b = threading.Lock()
>>> a
<thread.lock object at 0x1002ab190>
>>> b
<thread.lock object at 0x1002ab0f0>
I wonder if there’s any difference between these 2 creation methods?
Python Docs itself recommends the
threadingmodule.The
threadingmodule lays on top of thethreadmodule, as stated in the threading docsSo one would assume that the locks they use are very close to identical, but the
threadingmodule wraps thethreadmodules calls.Indeed later in the threading docs, it states:
So in summary, they are most likely identical, and python itself recommends the
threadingmodule. So use that if you are in doubt.