试图使用共享队列同时运行两个不同的函数,但出现错误...如何使用共享队列同时运行两个函数?这是Windows 7上的Python 3.6版.

from multiprocessing import Process
from queue import Queue
import logging

def main():
    x = DataGenerator()
    try:
        x.run()
    except Exception as e:
        logging.exception("message")


class DataGenerator:

    def __init__(self):
        logging.basicConfig(filename='testing.log', level=logging.INFO)

    def run(self):
        logging.info("Running Generator")
        queue = Queue()
        Process(target=self.package, args=(queue,)).start()
        logging.info("Process started to generate data")
        Process(target=self.send, args=(queue,)).start()
        logging.info("Process started to send data.")

    def package(self, queue): 
        while True:
            for i in range(16):
                datagram = bytearray()
                datagram.append(i)
                queue.put(datagram)

    def send(self, queue):
        byte_array = bytearray()
        while True:
            size_of__queue = queue.qsize()
            logging.info(" queue size %s", size_of_queue)
            if size_of_queue > 7:
                for i in range(1, 8):
                    packet = queue.get()
                    byte_array.append(packet)
                logging.info("Sending datagram ")
                print(str(datagram))
                byte_array(0)

if __name__ == "__main__":
    main()

日志(log)显示一个错误,我try 以管理员身份运行console,我收到了相同的消息...

INFO:root:Running Generator
ERROR:root:message
Traceback (most recent call last):
  File "test.py", line 8, in main
    x.run()
  File "test.py", line 20, in run
    Process(target=self.package, args=(queue,)).start()
  File "C:\ProgramData\Miniconda3\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)
  File "C:\ProgramData\Miniconda3\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\ProgramData\Miniconda3\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:\ProgramData\Miniconda3\lib\multiprocessing\popen_spawn_win32.py", line 65, in __init__
    reduction.dump(process_obj, to_child)
  File "C:\ProgramData\Miniconda3\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
TypeError: can't pickle _thread.lock objects

推荐答案

multiprocessing.Pool - PicklingError: Can't pickle <type 'thread.lock'>: attribute lookup thread.lock failed

将队列移动到self,而不是作为函数packagesend的参数

Python-3.x相关问答推荐

替换Pandas中组下的列值

是否有必要使用Threads()中的args显式地将共享变量传递给Python中的线程函数或直接访问它?

如何绘制交叉验证的AUROC并找到最佳阈值?

如何将项目添加到Python中具有固定大小的列表列表中

比较和排序 DataFrame 两列中的值并在 python 中的同一行中排序

不同的焦点顺序和堆叠顺序 tkinter

SQL Server 2022和Python3.10脚本错误

如何将函数映射到所有命名元组的元素?

为什么我不能通过索引获取字典键?

如何准确测定cv2的结果.在BW/黑白图像中查找对象?

有没有更好的方法来判断一个数字是否是两个数字的范围

django.core.exceptions.ImproperlyConfigured

理解 Keras 的 ImageDataGenerator 类中的 `width_shift_range` 和 `height_shift_range` 参数

如何模拟 Django 模型对象(及其方法)?

使用逗号时,除了处理程序中的语法无效

if 语句中冒号的语法错误

Tensorflow:ImportError:libcudnn.so.7:无法打开共享对象文件:没有这样的文件或目录

如何使用请求发送带有标头的 PATCH 请求

如何从集合中删除多个元素?

在 Visual Studio Code 中调试 Scrapy 项目