访问在QThread内部由Pynput侦听器调用的线程中访问的SQLite3数据库连接时关注竞争条件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了访问在QThread内部由Pynput侦听器调用的线程中访问的SQLite3数据库连接时关注竞争条件相关的知识,希望对你有一定的参考价值。

我正在用Pyside2编写Windows应用程序。由于我使用多线程的性质,我不得不在多个线程中与相同的Sqlite3数据库进行交互。我已经创建了一个<100行的Minimal, complete, verifiable example几乎完全复制了这个问题。

问题:一旦按下PushButton,我正在使用pynput module监视后台中的键活动,而Qt GUI没有聚焦“j”+“k”的热键组合。按下热键组合后,将截取屏幕截图,通过OCR处理图像并将其与OCR文本一起保存到数据库中。图像路径通过一系列连接信号发送到主GUI线程。密钥监控发生在另一个QThread中,以防止密钥监控和图像处理影响主Qt事件循环的运行。一旦QThread启动并发出它的开始信号,我就调用key_monitor实例中的monitor_for_hot_key_combo函数,该函数实例化listeneras一个threading.Thread,它被指定为key_monitor成员函数on_releaseon_press作为回调函数,每次按下一个键时都会调用它。

这就是问题所在。这些回调在与实例化的类不同的线程中与imageprocessing_objclass的image_process实例交互。因此,当image_process成员函数与使用SQlite数据库进行交互时,它们在一个单独的线程中执行,而不是在数据库连接中创建.Now, SQLite "can be safely used by multiple threads规定在两个或多个线程中不同时使用单个数据库连接“。为此,你必须将check_same_threadsqlite3.connect()参数设置为False。但是,如果可能的话,Id宁可避免对数据库的这种多线程访问以防止未定义的行为。

可能的解决方案:我一直想知道两个线程,threading.ThreadQThread都不是必需的,它们都可以在Pynput线程中完成。但是,我似乎无法弄清楚如何只使用Pynput线程,同时仍然能够将信号发送回主Qt事件循环。

强推.朋友

from PySide2 import QtCore, QtWidgets
from PySide2.QtCore import *
import HotKeyMonitor

class Ui_Form(object):
    def __init__(self):
        self.worker = None

    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.pressbutton = QtWidgets.QPushButton(Form)
        self.pressbutton.setObjectName("PushButton")
        self.pressbutton.clicked.connect(self.RunKeyMonitor)
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtWidgets.QApplication.translate("Form", "Form", None, -1))
        self.pressbutton.setText(QtWidgets.QApplication.translate("Form", "Press me", None, -1))

    def RunKeyMonitor(self):
        self.Thread_obj = QThread()
        self.HotKeyMonitor_Obj = HotKeyMonitor.key_monitor()
        self.HotKeyMonitor_Obj.moveToThread(self.Thread_obj)
        self.HotKeyMonitor_Obj.image_processed_km.connect(self.print_OCR_result)
        self.Thread_obj.started.connect(self.HotKeyMonitor_Obj.monitor_for_hotkey_combo)
        self.Thread_obj.start()

    def print_OCR_result(self, x):
        print("Slot being called to print image path string")
        print(x)
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

hotkey monitor.朋友

from pynput import keyboard
from PySide2.QtCore import QObject, Signal

import imageprocess
class key_monitor(QObject):
    image_processed_km = Signal(str)
    def __init__(self):
        super().__init__()
        self.prev_key = None
        self.listener = None
        self.imageprocessing_obj = imageprocess.image_process()
        self.imageprocessing_obj.image_processed.connect(self.image_processed_km.emit)


    def on_press(self,key):
        pass

    def on_release(self,key):
        if type(key) == keyboard._win32.KeyCode:

            if key.char.lower() == "j":
                self.prev_key = key.char.lower()
            elif key.char.lower() == "k" and self.prev_key == "j":
                print("key combination j+k pressed")
                self.prev_key = None
                self.imageprocessing_obj.process_image()
        else:
            self.prev_key = None

    def stop_monitoring(self):
        self.listener.stop()

    def monitor_for_hotkey_combo(self):
        with keyboard.Listener(on_press=self.on_press, on_release = self.on_release) as self.listener:self.listener.join()

image process.朋友

import uuid,os,sqlite3,pytesseract
from PIL import ImageGrab
from PySide2.QtCore import QObject, Signal

class image_process(QObject):
    image_processed = Signal(str)
    def __init__(self):
        super().__init__()
        self.screenshot = None
        self.db_connection = sqlite3.connect("testdababase.db", check_same_thread=False)
        self.cursor = self.db_connection.cursor()
        self.cursor.execute("CREATE TABLE IF NOT EXISTS testdb (OCRstring text, filepath text)")

    def process_image(self):
        self.screenshot = ImageGrab.grab()
        self.screenshot_path =  os.getcwd() + "\" + uuid.uuid4().hex + ".jpg"
        self.screenshot.save(self.screenshot_path )
        self.ocr_string = pytesseract.image_to_string(self.screenshot)
        self.cursor.execute("INSERT INTO testdb (OCRstring, filepath) VALUES (?,?)",(self.ocr_string, self.screenshot_path))
        self.image_processed.emit(self.screenshot_path)
答案

首先,QThread不是Qt线程,也就是说,它不是新类型的线程,QThread是一个管理每个平台的本机线程的类。因此处理QThread的线程具有与threading.Thread相同的特征。

另一方面,在GUI中使用线程的目的不是阻止称为GUI线程的主线程,在你的pynput中它已经有了它的线程所以没有问题。阻塞的另一个任务是OCR的任务,所以我们必须在新线程中执行它。数据库的任务并不昂贵,因此没有必要创建一个线程。

key monitor.朋友

from pynput import keyboard
import time
from PySide2 import QtCore

class KeyMonitor(QtCore.QObject):
    letterPressed = QtCore.Signal(str)

    def __init__(self, parent=None):
        super().__init__(parent)
        self.listener = keyboard.Listener(on_release = self.on_release)

    def on_release(self,key):
        if type(key) == keyboard._win32.KeyCode:
            self.letterPressed.emit(key.char.lower())

    def stop_monitoring(self):
        self.listener.stop()

    def start_monitoring(self):
        self.listener.start()

image process.朋友

import uuid
import pytesseract

from PIL import ImageGrab

from PySide2 import QtCore

class ProcessWorker(QtCore.QObject):
    processSignal = QtCore.Signal(str, str)

    def doProcess(self):
        screenshot = ImageGrab.grab()
        screenshot_path =  QtCore.QDir.current().absoluteFilePath(uuid.uuid4().hex+".jpg")
        screenshot.save(screenshot_path )
        print("start ocr")
        ocr_string = pytesseract.image_to_string(screenshot)
        print(ocr_string, screenshot_path)
        self.processSignal.emit(ocr_string, screenshot_path)
        self.thread().quit()

卖弄.朋友

from keymonitor import KeyMonitor
from imageprocess import ProcessWorker
from PySide2 import QtCore, QtWidgets

import sqlite3

class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.last_letter = ""
        self.current_letter = ""

        lay = QtWidgets.QVBoxLayout(self)
        button = QtWidgets.QPushButton("Start")
        button.clicked.connect(self.onClicked)
        lay.addWidget(button)

        self.keymonitor = KeyMonitor()
        self.keymonitor.letterPressed.connect(self.onLetterPressed)

        self.db_connection = sqlite3.connect("testdababase.db")
        self.cursor = self.db_connection.cursor()
        self.cursor.execute("CREATE TABLE IF NOT EXISTS testdb (OCRstring text, filepath text)")
        self.threads = []

    def onClicked(self):
        self.keymonitor.start_monitoring()

    def onLetterPressed(self, letter):
        if self.last_letter:
            if self.current_letter:
                self.last_letter = self.current_letter
            self.current_letter = letter
        else:
            self.last_letter = letter

        if self.last_letter == "j" and self.current_letter == "k":
            print("j+k")
            self.start_processing()

    def start_processing(self):
        thread = QtCore.QThread()
        self.worker = ProcessWorker()
        self.worker.processSignal.connect(self.onProcessSignal)
        self.worker.moveToThread(thread)
        thread.started.connect(self.worker.doProcess)
        thread.finished.connect(self.worker.deleteLater)
        thread.finished.connect(lambda th=thread: self.threads.remove(th))
        thread.start()
        self.threads.append(thread)

    def onProcessSignal(self, ocr, path):
        print(ocr, path)
        self.cursor.execute("INSERT INTO testdb (OCRstring, filepath) VALUES (?,?)",(ocr, path))
        self.db_connection.commit()

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

以上是关于访问在QThread内部由Pynput侦听器调用的线程中访问的SQLite3数据库连接时关注竞争条件的主要内容,如果未能解决你的问题,请参考以下文章

PyQt - 主要挂在 QThread

将参数传递给 pynput 侦听器

pynput.mouse 监听器不停止

使用 pynput 监听器创建键盘快捷键

比较从 pynput 返回的数据

如何使用pytest正确退出队列和Qthread进行测试?