首页 > 解决方案 > 如何在 Raspberry Pi 4 中为多个输入设备执行多线程

问题描述

我正在尝试使用 raspberry pi 4 从条形码扫描仪、称重秤和相机获取输入。我想我将为每个输入使用多线程。我分别测试了每个输入代码并且它们工作但是每当我尝试为所有它们运行多线程(现在只是条形码,称重秤和 html)时,我都会收到此错误

我在这里使用这个条形码扫描仪

这个体重秤在这里

Traceback (most recent call last):
  File "/usr/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
    self.run()
  File "/usr/lib/python3.7/multiprocessing/process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "/home/pi/Documents/Konstant/py/cartos.py", line 142, in initiateBarcodeReader
    s = readBarcode("/dev/input/event0")
  File "/home/pi/Documents/Konstant/py/cartos.py", line 41, in readBarcode
    dev.grab() # grab provides exclusive access to the device
  File "/home/pi/.local/lib/python3.7/site-packages/evdev/device.py", line 320, in grab 
    _input.ioctl_EVIOCGRAB(self.fd, 1)
OSError: [Errno 16] Device or resource busy

这是我的代码的一部分,它显示了打开 html 页面以及我如何调用多线程。我不确定是否需要包括条形码扫描仪和体重秤代码,因为它们是分开工作的。如果需要,条形码扫描仪代码在这里

import eel
import os 
import json
import requests
import shutil, sys 
import os 
from evdev import InputDevice, categorize, ecodes  
import threading
import time

    def openUI():
        eel.init("../web")
        eel.start('home.html')


    if __name__ == '__main__':
        
        ui_thread = Process(target=openUI, args=('UI opened',))
        p = Process(target=startWeighingScale, args=('weighing started',))
        zp = Process(target=initiateBarcodeReader, args=('Barcode called',))
        
        ui_thread.start()
        zp.start()

这是同时从这些输入中获取数据的最佳方法吗?如何修复错误?

标签: pythonmultithreadingraspberry-pi

解决方案


推荐阅读