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



编辑

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

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:UsersambujAnaconda3libmultiprocessingprocess.py", line 112, in start
self._popen = self._Popen(self)
File "C:UsersambujAnaconda3libmultiprocessingcontext.py", line 223, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:UsersambujAnaconda3libmultiprocessingcontext.py", line 322, in _Popen
return Popen(process_obj)
File "C:UsersambujAnaconda3libmultiprocessingpopen_spawn_win32.py", line 89, in __init__
reduction.dump(process_obj, to_child)
File "C:UsersambujAnaconda3libmultiprocessingreduction.py", line 60, in dump
ForkingPickler(file, protocol).dump(obj)
ValueError: ctypes objects containing pointers cannot be pickled
PS C:UsersambujDocumentsPython Scripts> Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:UsersambujAnaconda3libmultiprocessingspawn.py", line 99, in spawn_main
new_handle = reduction.steal_handle(parent_pid, pipe_handle)
File "C:UsersambujAnaconda3libmultiprocessingreduction.py", line 82, in steal_handle
_winapi.PROCESS_DUP_HANDLE, False, source_pid)
OSError: [WinError 87] The parameter is incorrect

基本上我在做什么/想做

我有一个不断发送数据的发射机,我有一台不断接收数据的接收机。当我发送数据时,我记下时间,当我接收数据时,记下时间。脚本正在连续运行。我正在并行运行这些脚本。由于我想同时运行这些脚本,所以我正在使用多处理。

感谢

我相信您不需要对此类操作使用多处理模块,它用于增强计算。我想你想在一个过程中从一个端口读取并写入另一个端口,否则你可以只写入不同的程序并独立运行它们。对于IN/OUT,使用螺纹模块。

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

这是我的信使程序

我想代码可以这样工作:

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()

相关内容

  • 没有找到相关文章

最新更新