Custom Locks Threading python

5.6k views Asked by At

Hi I am using PyQt4 and i need to implement locks in a QThread but this class does not have a method lock implemented like the library threading. Any idea how can I implement a lock here?

I have a question if i use threading i implement the lock like this

class Example:
     lock = threading.Lock()
     def __init__(self)
         pass
     def run(self):
         Example.lock.acquire()
         .......
         ........
         Example.lock.realease()

is this the same?:

class Example(QtCore.QThread):
     mutex = QtCore.QMutex())
     def __init__(self)
         pass
     def run(self):
         mutex.lock()
         .......
         ........
         mutex.unlock()

Thanks

1

There are 1 answers

3
Alex Huszagh On BEST ANSWER

You want the QMutex class. Qt uses the QtCore.QMutex.lock() and unlock() functions to lock and unlock QThreads.

Here's an example: https://github.com/Werkov/PyQt4/blob/master/examples/threads/waitconditions.py

EDIT: They're fairly similar although there are subtle differences.

http://www.laurentluce.com/posts/python-threads-synchronization-locks-rlocks-semaphores-conditions-events-and-queues/

http://doc.qt.io/qt-4.8/qmutex.html

The QMutex class effective can support both threading.Lock and threading.RLock behavior. Both these should thoroughly explain their use, limitations in standard implementations.

The QMutex reference for standard Qt (so it's written in C++), but the same principles hold.

EDIT2:

Adding in an example for both using the standard threading module and the QMutex class:

from PySide import QtCore

mutex = QtCore.QMutex()

class QtLock(QtCore.QThread):

    def __init__(self, name):
        super(QtLock, self).__init__()
        self.name = name

    def run(self):

        for i in range(10):
            mutex.lock()
            print i, self.name,
            mutex.unlock()

threads = []
for i in range(5):
    name = chr(i + ord('a'))
    threads.append(QtLock(name))

for thread in threads:
    thread.start()

for thread in threads:
    thread.wait()

When I run the QMutex example, I get the following:

0 a 0 d 1 a 0 b 2 a 1 b 2 b 1 d 3 a 3 b 0 c 4 a 4 b 1 c 2 d 2 c 3 d 4 d 5 b 3 c 6 b 5 d 4 c 6 d 7 b 7 d 8 d 5 c 8 b 9 d 6 c 9 b 7 c 8 c 9 c 0 e 1 e 2 e 5 a 3 e 6 a 4 e 7 a 5 e 6 e 8 a 7 e 9 a 8 e 9 e

When I comment out the .lock() and .unlock() lines, I get this, showing how the QMutex effectively acquires and releases the lock:

00  cd  1 d 2 0 e 1 e 21d  ec  33 e 4 e  5  00 bd2   a 1 ec a4 d3   6  c 2154 d  b  a c 3   6 d 2e75  db  8 7a3    e 8  ce 9  b4 4ed b6 c 7   a  55 9b c d a8 c 6 9  6 b 7 b 8 b 9 c ba 7 a 8 a 9 a

Meanwhile, here I have almost the exact same code for the standard threading module:

import threading

lock = threading.Lock()

class PyThread(threading.Thread):

    def __init__(self, name):
        super(PyThread, self).__init__()
        self.name = name

    def run(self):

        for i in range(10):
            lock.acquire()
            print i, self.name,
            lock.release()

threads = []
for i in range(5):
    name = chr(i + ord('a'))
    threads.append(PyThread(name))

for thread in threads:
    thread.start()

for thread in threads:
    thread.join()

And the output is:

0 a 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 a 0 b 1 b 2 b 3 b 4 b 5 b 0 c 1 c 6 b 7 b 8 b 9 b 2 c 3 c 4 c 5 c 6 c 0 d 7 c 8 c 0 e 1 e 9 c 1 d 2 e 2 d 3 d 4 d 5 d 6 d 7 d 8 d 9 d 3 e 4 e 5 e 6 e 7 e 8 e 9 e

Likewise, when I comment out the lock.acquire() and lock.release(), I get:

0 a 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 a 0 b 1 b 2 b 3 b 4 b 5 b 6 b 7 b 8 b 9 b 0 c 1 0 dc 2 c 3 c 4 c 5 1  d 2c 0 e  16 ed   c 72  c e8 3  ec 3 d4   e4 9  5cd  5e  d 6 6e d 7 d  87  e 8 ed 9 e 9 d