Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在python中实现多处理(参数不正确)_Python_Python 3.x_Multithreading_Multiprocessing - Fatal编程技术网

无法在python中实现多处理(参数不正确)

无法在python中实现多处理(参数不正确),python,python-3.x,multithreading,multiprocessing,Python,Python 3.x,Multithreading,Multiprocessing,编辑 我正在尝试使用多处理同时运行函数 import serial import time from multiprocessing import Process import sys sertx = serial.Serial('COM4', 115200) serrx = serial.Serial('COM3', 115200) rx_t=0 tx_t=0 def rx(serrx): global rx_t while True: print(

编辑

我正在尝试使用多处理同时运行函数

import serial
import time

from multiprocessing import Process
import sys

sertx = serial.Serial('COM4', 115200)
serrx = serial.Serial('COM3', 115200)

rx_t=0
tx_t=0

def rx(serrx):

    global rx_t

    while True:
        print("hi")
        read_serial=serrx.readline()
        rx_t = time.time()
        print(read_serial)
        print('rx: ',rx_t)

def tx(sertx):
    print("started")
    global tx_t
    while True:

        msg = str(1)
        # print('sending: ',msg.encode())

        msgstat = 'A' + msg
        #print(msgstat)
        #print(type(msgstat))
        tx_t = time.time()
        sertx.write(msg.encode())
        print('tx: ',tx_t)

if __name__ == '__main__': 
    p1 = Process(target=tx,args=(sertx,))
    p2 = Process(target=rx,args=(serrx,))
    p1.start()
    p2.start()
    p1.join()
    p2.join()
错误

Traceback (most recent call last):
  File "c:/Users/ambuj/Documents/Python Scripts/wave.py", line 58, in <module>
    p1.start()
  File "C:\Users\ambuj\Anaconda3\lib\multiprocessing\process.py", line 112, in start
    self._popen = self._Popen(self)
  File "C:\Users\ambuj\Anaconda3\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Users\ambuj\Anaconda3\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:\Users\ambuj\Anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 89, in __init__
    reduction.dump(process_obj, to_child)
  File "C:\Users\ambuj\Anaconda3\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
ValueError: ctypes objects containing pointers cannot be pickled
PS C:\Users\ambuj\Documents\Python Scripts> Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\ambuj\Anaconda3\lib\multiprocessing\spawn.py", line 99, in spawn_main
    new_handle = reduction.steal_handle(parent_pid, pipe_handle)
  File "C:\Users\ambuj\Anaconda3\lib\multiprocessing\reduction.py", line 82, in steal_handle
    _winapi.PROCESS_DUP_HANDLE, False, source_pid)
OSError: [WinError 87] The parameter is incorrect

回溯(最近一次呼叫最后一次):
文件“c:/Users/ambuj/Documents/Python Scripts/wave.py”,第58行,在
p1.开始()
文件“C:\Users\ambuj\Anaconda3\lib\multiprocessing\process.py”,第112行,在开始处
self.\u popen=self.\u popen(self)
文件“C:\Users\ambuj\Anaconda3\lib\multiprocessing\context.py”,第223行,在\u Popen中
返回_default_context.get_context().Process._Popen(Process_obj)
文件“C:\Users\ambuj\Anaconda3\lib\multiprocessing\context.py”,第322行,在\u Popen中
返回Popen(过程对象)
文件“C:\Users\ambuj\Anaconda3\lib\multiprocessing\popen\u spawn\u win32.py”,第89行,在\uu init中__
减少.转储(进程对象,到子进程)
文件“C:\Users\ambuj\Anaconda3\lib\multiprocessing\reduce.py”,第60行,转储文件
ForkingPickler(文件、协议).dump(obj)
ValueError:无法pickle包含指针的ctypes对象
PS C:\Users\ambuj\Documents\Python Scripts>Traceback(最后一次调用):
文件“”,第1行,在
文件“C:\Users\ambuj\Anaconda3\lib\multiprocessing\spawn.py”,第99行,在spawn\u main中
新的\u句柄=减少。偷取\u句柄(父\u pid,管道\u句柄)
文件“C:\Users\ambuj\Anaconda3\lib\multiprocessing\reduce.py”,第82行,在steal\u句柄中
_winapi.PROCESS\u DUP\u HANDLE,False,source\u pid)
OSError:[WinError 87]参数不正确
基本上我正在做/想做的事

我有一个持续传输数据的发射器和一个持续接收数据的接收器。当我传输数据时,我记下时间,当我收到数据时,我记下时间。脚本持续运行。我正在并行运行这些脚本。因为我想同时运行这些脚本,所以我使用多处理


谢谢

我确信您不需要为此类操作使用多处理模块,它用于提高计算。我猜您希望在一个进程中从一个端口读取数据并向另一个端口写入数据,否则您可以只向不同的程序写入数据并独立运行它们。用于输入/输出线程模块

我从未使用过串行库,但知道如何创建进程,在一个线程中侦听输入并打印到另一个线程:

我想代码可以这样工作:

import serial
import time
from threading import Thread


def rx(_ser_rx):
    while True:
        print("hi")
        read_serial = _ser_rx.readline()
        rx_t = time.time()
        print(read_serial)
        print('rx: ', rx_t)


def tx(_ser_tx):
    print("started")
    while True:
        msg = "1"
        tx_t = time.time()
        _ser_tx.write(msg.encode())
        print('tx: ', tx_t)


if __name__ == '__main__':
    ser_tx = serial.Serial('COM4', 115200)
    ser_rx = serial.Serial('COM3', 115200)
    t1 = Thread(target=rx, args=(ser_rx,),)
    t2 = Thread(target=tx, args=(ser_tx,))
    t1.start()
    t2.start()

我想这不是多处理器问题:
raiseserialexception(“无法打开端口{!r}:{!r}”)。格式(self.portstr,ctypes.WinError())
我可以打开端口individually@ArtiomKozyrev我在函数中传递了ports变量,但出现了另一个错误。不知道您的代码在做什么,但请检查您是否向进程传递了正确的参数?在函数定义中,您将serrx传递给rx,但在定义过程时,您将sertx传递给它。逻辑错误。@AmbujeGupta如果不使用多处理模块,当您使用一个com端口时,您的代码是否工作?谢谢,伙计,它工作得很好,我已经为此工作了一天多了。谢谢。你能回答我的一些问题吗?我的代码中有什么错误?这将有助于提高我的理解。@AmbujeGupta多处理模块使用单独的内存堆栈创建单独的进程,要将数据从一个进程传输到另一个进程,您需要序列化(“反汇编”)它,您的串行端口对象是一个困难的工作人员,而另一个进程不知道这是什么以及如何组装它。另一方面,多线程模块是一个进程=一个内存堆栈,因此任何线程都可以访问堆栈中的任何对象。