Execute command to a Python script from separate Python script?

点点圈 提交于 2021-01-29 07:26:23

问题


I'm trying to send a command from one python script to another running in terminal.

I'm running two python scripts on an RPi running Raspbian. The first script is a loop that waits for the user to enter a number and adds it to a total. The second script uses PySide2 to print a number when a QPushButton is released.

How can i make it so that the function that runs when the QPushButton is released, sends a command (or variable) into the waiting first script and executes it?

I've read some things about using subprocess.call and os.system but i'm not really sure what i'm doing with these commands, or if they're appropriate for what i want to do.

First script:

x = 0
while x < 10:
    y = int(input("enter number:"))
    x += y
    print(x)

print("x is ten!")

Second script:

import sys
from PySide2.QtWidgets import *

class MainWindow(QWidget):
    def __init__(self):
        QWidget.__init__(self)

        self.btn = QPushButton("test")

        layout = QVBoxLayout()
        layout.addWidget(self.btn)

        self.setLayout(layout)

        self.btn.released.connect(self.btnpress)

    def btnpress(self):
        print(1)

app = QApplication(sys.argv)

window = MainWindow()
window.show()
app.exec_()


回答1:


If you want to run a script and send information through stdin then in Qt the best option is to use QProcess:

import os.path
import sys

from PySide2.QtCore import QProcess
from PySide2.QtWidgets import QApplication, QPushButton, QSpinBox, QVBoxLayout, QWidget

CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))


class MainWindow(QWidget):
    def __init__(self):
        QWidget.__init__(self)

        self.btn = QPushButton("test")
        self.spinbox = QSpinBox()

        self.process = QProcess()
        self.process.readyReadStandardError.connect(self.handle_readyReadStandardError)
        self.process.readyReadStandardOutput.connect(
            self.handle_readyReadStandardOutput
        )
        self.process.setProgram(sys.executable)
        script_path = os.path.join(CURRENT_DIR, "script.py")
        self.process.setArguments([script_path])
        self.process.start()

        layout = QVBoxLayout(self)
        layout.addWidget(self.spinbox)
        layout.addWidget(self.btn)
        self.btn.released.connect(self.btnpress)

    def btnpress(self):
        number = self.spinbox.value()

        msg = "{}\n".format(number)
        self.process.write(msg.encode())

    def handle_readyReadStandardError(self):
        print(self.process.readAllStandardError().data().decode())

    def handle_readyReadStandardOutput(self):
        print(self.process.readAllStandardOutput().data().decode())


app = QApplication(sys.argv)

window = MainWindow()
window.show()
app.exec_()

Note: You don't have to run the script in a second shell as the script itself starts it.




回答2:


Subprocesses make sense when there is a clear parent/child relationship. This sounds more like you have two independent agents which occasionally need to communicate. There are various inter-process communication (IPC) mechanisms depending on your needs, the most basic being signals, where either process can send the other a single bit of information to notify it that some event occurred. For more complex needs, set up shared memory, or talk over a socket or a FIFO.




回答3:


i think that you have to add a third party, like:

  1. DATABASE
  2. REDIS
  3. File

but you cannot not send a command from a running script to another running script, because the CLI script wait command from his shell only. greetings



来源:https://stackoverflow.com/questions/64515325/execute-command-to-a-python-script-from-separate-python-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!