gpt4 book ai didi

python-3.x - 类型错误 : can't pickle _thread. 锁定对象

转载 作者:行者123 更新时间:2023-12-03 10:16:12 25 4
gpt4 key购买 nike

尝试使用共享队列同时运行两个不同的函数并出现错误...如何使用共享队列同时运行两个函数?这是 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()

日志指示错误,我尝试以管理员身份运行控制台,但收到相同的消息...
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 - 类型错误 : can't pickle _thread. 锁定对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44144584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com