具有子处理功能的多处理
Multiprocessing with subprocessing functions
我正在编写一个程序,该程序具有使用子进程处理 receieve/transmit 消息的功能,但是,我希望能够在后台运行接收,同时能够打印接收到的任何内容。
请看代码:
#!/usr/bin/python
import subprocess,time, timeit
from multiprocessing import Process, Queue
import re, os, pprint, math
from collections import defaultdict
Dict = {}
identifier = ""
hexbits = []
count = defaultdict(int)
def receive():
process = subprocess.Popen('receivetest -f=/dev/pcan32', stdout=subprocess.PIPE)
lines = iter(process.stdout.readline, "")
try:
start = time.clock()
for line in lines:
if re.match(r"^\d+.*$",line):
splitline = line.split()
del splitline[1:4]
identifier = splitline[1]
count[identifier] += 1
end = time.clock()
timing = round((end - start) * 10000, 100)
dlc = splitline[2]
hexbits = splitline[3:]
Dict[identifier] = [dlc, hexbits, count[identifier],int(timing)]
time.sleep(0.005)
start = end
except keyboardinterrupt:
pass
def transmit(command):
transfile = "transfile.txt"
file = open(transfile,'w')
file.write("# please type your can message below\n")
file.write("# using the following example\n")
file.write("# m s [can id] [data bytes]\n")
file.close()
os.system("vi transfile.txt")
trans = subprocess.Popen(command, stdout=subprocess.pipe)
lines = trans.communicate()
print lines
operation = raw_input('please enter T for transmit and R for receive: ')
if((operation == 'T') or (operation == 't')):
transmit(["transmitest", "transfile.txt","-f=/dev/pcan32"])
elif((operation == 'R') or (operation == 'r')):
if __name__ == '__main__':
os.system("echo \"i 0x011c e\" >/dev/pcan32")
#receive(["receivetest", "-f=/dev/pcan32"])
run_receive = Process(target=receive, args=())
run_receive.start()
run_receive.join()
for identifier, hexbits in Dict.items():
os.system("clear")
pprint.pprint(Dict, width = 150)
else:
print("PLEASE ENTER VALID OPTION")
但是我收到以下错误:
Trace (most recent call last):
File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in run
self._target(*self._args, **self._kwargs)
File "./cancheck.py", line 16, in receive
process = subprocess.Popen(command, stdout=subprocess.PIPE)
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 679, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
有人可以帮忙吗,我不确定如何对包含子进程的函数进行多处理。
子进程命令调用似乎无法解析文件。我建议验证 transmitest
在执行路径中,并且 transfile.txt
和 /dev/pcan32
都存在。
另外请注意,在 receive
定义中,您的 Popen
调用看起来需要更改。
# Original version
# process = subprocess.Popen('receivetest -f=/dev/pcan32', stdout=subprocess.PIPE)
# Change to this:
process = subprocess.Popen('receivetest -f=/dev/pcan32'.split(), stdout=subprocess.PIPE)
原因是 Popen 需要一个可迭代序列作为它的第一个参数。
我正在编写一个程序,该程序具有使用子进程处理 receieve/transmit 消息的功能,但是,我希望能够在后台运行接收,同时能够打印接收到的任何内容。
请看代码:
#!/usr/bin/python
import subprocess,time, timeit
from multiprocessing import Process, Queue
import re, os, pprint, math
from collections import defaultdict
Dict = {}
identifier = ""
hexbits = []
count = defaultdict(int)
def receive():
process = subprocess.Popen('receivetest -f=/dev/pcan32', stdout=subprocess.PIPE)
lines = iter(process.stdout.readline, "")
try:
start = time.clock()
for line in lines:
if re.match(r"^\d+.*$",line):
splitline = line.split()
del splitline[1:4]
identifier = splitline[1]
count[identifier] += 1
end = time.clock()
timing = round((end - start) * 10000, 100)
dlc = splitline[2]
hexbits = splitline[3:]
Dict[identifier] = [dlc, hexbits, count[identifier],int(timing)]
time.sleep(0.005)
start = end
except keyboardinterrupt:
pass
def transmit(command):
transfile = "transfile.txt"
file = open(transfile,'w')
file.write("# please type your can message below\n")
file.write("# using the following example\n")
file.write("# m s [can id] [data bytes]\n")
file.close()
os.system("vi transfile.txt")
trans = subprocess.Popen(command, stdout=subprocess.pipe)
lines = trans.communicate()
print lines
operation = raw_input('please enter T for transmit and R for receive: ')
if((operation == 'T') or (operation == 't')):
transmit(["transmitest", "transfile.txt","-f=/dev/pcan32"])
elif((operation == 'R') or (operation == 'r')):
if __name__ == '__main__':
os.system("echo \"i 0x011c e\" >/dev/pcan32")
#receive(["receivetest", "-f=/dev/pcan32"])
run_receive = Process(target=receive, args=())
run_receive.start()
run_receive.join()
for identifier, hexbits in Dict.items():
os.system("clear")
pprint.pprint(Dict, width = 150)
else:
print("PLEASE ENTER VALID OPTION")
但是我收到以下错误:
Trace (most recent call last):
File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in run
self._target(*self._args, **self._kwargs)
File "./cancheck.py", line 16, in receive
process = subprocess.Popen(command, stdout=subprocess.PIPE)
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 679, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
有人可以帮忙吗,我不确定如何对包含子进程的函数进行多处理。
子进程命令调用似乎无法解析文件。我建议验证 transmitest
在执行路径中,并且 transfile.txt
和 /dev/pcan32
都存在。
另外请注意,在 receive
定义中,您的 Popen
调用看起来需要更改。
# Original version
# process = subprocess.Popen('receivetest -f=/dev/pcan32', stdout=subprocess.PIPE)
# Change to this:
process = subprocess.Popen('receivetest -f=/dev/pcan32'.split(), stdout=subprocess.PIPE)
原因是 Popen 需要一个可迭代序列作为它的第一个参数。