gpt4 book ai didi

python - 具有多处理功能的 pyserial 给了我一个 ctype 错误

转载 作者:行者123 更新时间:2023-12-04 12:02:44 28 4
gpt4 key购买 nike

嗨,我正在尝试编写一个模块,让我通过 pyserial 读取和发送数据.我必须能够与我的主脚本并行读取数据。在 stackoverflow 用户的帮助下,我有一个基本的工作框架 the program ,但是当我尝试添加一个使用 pyserial(查找端口、速度等的句柄)创建的类时,发现 here我收到以下错误:

File "<ipython-input-1-830fa23bc600>", line 1, in <module>
runfile('C:.../pythonInterface1/Main.py', wdir='C:/Users/Daniel.000/Desktop/Daniel/Python/pythonInterface1')

File "C:...\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)

File "C:...\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Daniel.000/Desktop/Daniel/Python/pythonInterface1/Main.py", line 39, in <module>
p.start()

File "C:...\Anaconda3\lib\multiprocessing\process.py", line 112, in start
self._popen = self._Popen(self)

File "C:...\Anaconda3\lib\multiprocessing\context.py", line 223, in _Popen
return _default_context.get_context().Process._Popen(process_obj)

File "C:...\Anaconda3\lib\multiprocessing\context.py", line 322, in _Popen
return Popen(process_obj)

File "C:...\Anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 89, in __init__
reduction.dump(process_obj, to_child)

File "C:...\Anaconda3\lib\multiprocessing\reduction.py", line 60, in dump
ForkingPickler(file, protocol).dump(obj)

ValueError: ctypes objects containing pointers cannot be pickled

这是我用来调用 SerialConnection.py 中的类的代码
import multiprocessing 
from time import sleep
from operator import methodcaller

from SerialConnection import SerialConnection as SC

class Spawn:
def __init__(self, _number, _max):
self._number = _number
self._max = _max
# Don't call update here

def request(self, x):
print("{} was requested.".format(x))

def update(self):
while True:
print("Spawned {} of {}".format(self._number, self._max))
sleep(2)

if __name__ == '__main__':
'''
spawn = Spawn(1, 1) # Create the object as normal
p = multiprocessing.Process(target=methodcaller("update"), args=(spawn,)) # Run the loop in the process
p.start()
while True:
sleep(1.5)
spawn.request(2) # Now you can reference the "spawn"
'''
device = SC()
print(device.Port)
print(device.Baud)
print(device.ID)
print(device.Error)
print(device.EMsg)
p = multiprocessing.Process(target=methodcaller("ReadData"), args=(device,)) # Run the loop in the process
p.start()
while True:
sleep(1.5)
device.SendData('0003')

我做错了什么让这门课给我带来问题?使用 pyserial 是否有某种形式的限制?和多处理一起?我知道可以,但我不明白怎么做...

这是我从 python 得到的回溯
Traceback (most recent call last):   File "C:...\Python\pythonInterface1\Main.py", line 45, in <module>
p.start()

File "C:...\AppData\Local\Programs\Python\Python36-32\lib\multiprocessing\process.py", line 105, in start
self._popen = self._Popen(self)

File "C:...\AppData\Local\Programs\Python\Python36-32\lib\multiprocessing\context.py", line 223, in _Popen
return _default_context.get_context().Process._Popen(process_obj)

File "C:...\AppData\Local\Programs\Python\Python36-32\lib\multiprocessing\context.py", line 322, in _Popen
return Popen(process_obj)

File "C:...\AppData\Local\Programs\Python\Python36-32\lib\multiprocessing\popen_spawn_win32.py", line 65, in __init__
reduction.dump(process_obj, to_child)

File "C:...\AppData\Local\Programs\Python\Python36-32\lib\multiprocessing\reduction.py", line 60, in dump
ForkingPickler(file, protocol).dump(obj) ValueError: ctypes objects containing pointers cannot be pickled

最佳答案

您正在尝试传递 SerialConnection实例到另一个进程作为参数。因为 python 必须首先序列化(pickle)对象,而 SerialConnection 是不可能的。对象。

正如 Rob Streeting's answer 中所说,一个可能的解决方案是允许 SerialConnection使用 multiprocessing.Process.start 时发生的 fork 将对象复制到其他进程的内存被调用,但这在 Windows 上不起作用,因为它不使用 fork。

在代码中实现并行性的更简单、跨平台和更有效的方法是使用线程而不是进程。对代码的更改很少:

import threading
p = threading.Thread(target=methodcaller("ReadData"), args=(device,))

关于python - 具有多处理功能的 pyserial 给了我一个 ctype 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57640312/

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