简体   繁体   English

开始一个新过程并读取该过程的输出

[英]Start a new process and read that process's output

I've got a python script reads data from a piped bash script 我有一个python脚本从管道bash脚本读取数据

sudo beacon scan -b | get.py

I want to eliminate the use of bash piping and write everything in python. 我想消除对bash管道的使用,并使用python编写所有内容。 I'm trying to open a few processes in parallel to get my program to work the way I need it to. 我正在尝试同时打开一些进程,以使我的程序按照我需要的方式工作。 Process1 needs to start the 'sudo beacon scan -b' command and keep it running. Process1需要启动“ sudo beacon scan -b”命令并使其运行。 Process2 needs to send that data to my servers. Process2需要将该数据发送到我的服务器。 The last process needs to monitor if the process1 or process2 are working, and if not reboot them. 最后一个进程需要监视process1或process2是否正在运行,如果不重新启动它们。

I've written process1 and process2. 我已经写了process1和process2。

process1: 过程1:

#!/usr/bin/python
import subprocess
import multiprocessing

cmd = ['sudo', '/usr/local/bin/beacon', 'scan', '-b']
def scan():
   x = multiprocessing.Process(args=(subprocess.Popen(cmd)))
   x.start()
   x.join()

scan()

This runs the process correctly, but how do I get my second set of code the read the data from process1? 这样可以正确运行该进程,但是如何获取第二组代码以从process1中读取数据呢?

process2 过程2

import requests

for line in fileinput.input() #This needs to call the other process?
   if line.startswithh('ibeacon'):
      line = line.strip()
      a = line.split(' ')[1]
      a = line.split(' ')[2]
      a = line.split(' ')[3]
      a = line.split(' ')[4]
      a = line.split(' ')[5]
      payload = {'uuid': a, 'major': b, 'minor': c, 'power': d, 'rssi': e}
      r = requests.get('http://posttestserver.com/post.php', params=payload)

I don't know how to code process3 yet, but I need process1&2 to work first. 我还不知道如何编写process3的代码,但是我需要首先进行process1&2的工作。 How do I get process2 to read data from process1? 如何获取process2以便从process1读取数据? I'm also getting an error from process1 saying the 'Popen' object is not iterable, but it still runs the 'beacon scan' command. 我也从process1中收到一条错误消息,说“ Popen”对象不可迭代,但它仍运行“ beacon scan”命令。 Could this be because the 'beacon scan' command is constantly updating? 可能是因为“信标扫描”命令不断更新吗? Any advice would be helpful. 任何意见将是有益的。 I'm using Rasbian on a Raspberry Pi 我在Raspberry Pi上使用Rasbian

The following code does what you want: 以下代码可以满足您的要求:

1) it runs a script as a subprocess. 1)它将脚本作为子进程运行。 (I changed it to run "ping" for 3 seconds, for testing purposes.) (出于测试目的,我将其更改为“ ping”运行3秒钟。)

2) as each line is emitted from the subprocess, the parent proc handles it. 2)当每行从子流程中发出时,父proc处理它。 In this case, it calls send_beacon() , transmitting some data to another server. 在这种情况下,它将调用send_beacon() ,将一些数据传输到另一台服务器。

3) there is no #3. 3)没有#3。 Profit! 利润!

source 资源

import requests, subprocess

def send_beacon(line):
    if line.startswith('ibeacon'):
          line = line.strip()
          a = line.split(' ')[1]
          a = line.split(' ')[2]
          a = line.split(' ')[3]
          a = line.split(' ')[4]
          a = line.split(' ')[5]
          payload = {'uuid': a, 'major': b, 'minor': c, 'power': d, 'rssi': e}
          r = requests.get('http://posttestserver.com/post.php', params=payload)

if __name__=='__main__':
    if 0:
        cmd = ['sudo', '/usr/local/bin/beacon', 'scan', '-b']
    else:                       # testing
        cmd = 'ping -c3 8.8.8.8'.split()
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    for line in iter(proc.stdout.readline, ''):
        print 'X:',line,
        send_beacon(line)

output 产量

X: PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
X: 64 bytes from 8.8.8.8: icmp_seq=1 ttl=43 time=46.9 ms
X: 64 bytes from 8.8.8.8: icmp_seq=2 ttl=43 time=94.5 ms
X: 64 bytes from 8.8.8.8: icmp_seq=3 ttl=43 time=48.1 ms
X: 
X: --- 8.8.8.8 ping statistics ---
X: 3 packets transmitted, 3 received, 0% packet loss, time 2002ms
X: rtt min/avg/max/mdev = 46.922/63.221/94.554/22.161 ms

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM