简体   繁体   English

Python脚本触发另一个脚本中的方法

[英]Python script trigger a method in another script

I have two scripts say script1.py on PC1 and script2.py on PC2. 我有两个脚本,说PC1上的script1.py和PC2上的script2.py。 Now, script1.py has two methods as shown below. 现在,script1.py具有两种方法,如下所示。 The method1() on PC1 must be completed before method2() on PC2 can be executed. 必须先完成PC1上的method1()才能执行PC2上的method2()。 So, how can I trigger method2() of script2.py on PC2 which is already running? 那么,如何在已经运行的PC2上触发script2.py的method2()?

I can split script2.py into two different .py files ie method1.py and method2.py on PC2 and call method2.py after script1_method2() on PC1 has finished but then I need to pass the foo object to it. 我可以将script2.py拆分为两个不同的.py文件,即PC2上的method1.py和method2.py,并在PC1上的script1_method2()完成后调用method2.py,但是然后我需要将foo对象传递给它。 Maybe I can pickle the foo object and read it from a file? 也许我可以腌制foo对象并从文件中读取它?

Is there a better way to go about this? 有没有更好的方法来解决这个问题?

Note: I can communicate between PC1 and PC2 using paramiko ssh 注意:我可以使用paramiko ssh在PC1和PC2之间进行通信

def script1_method1():
    """
    This is on PC1
    get connection from PC2 script 2 and do some work
    """
    pass

def script1_method2():
    """
    This is on PC1
    work which needs some time
    only after this work is done, call PC2 script2
    """
    pass

def script2_method1():
    """
    This is on PC2
    create an object 'foo' which connects to PC1 script1
    return object 'foo' to main
    """
    pass



def script2_method2(foo):
    """
    This is on PC2
    get object 'foo' and do some work with it
    """
    pass

If you can mount PC2 dir with script2.py (for example via sshfs) you can add this dir to PYTHONPATH, then write a simple program: 如果可以使用script2.py(例如,通过sshfs)挂载PC2目录,则可以将此目录添加到PYTHONPATH中,然后编写一个简单程序:

from script1 import *
from script2 import *

script1_method1()
script2_method1()
script1_method2()
script2_method2()

This is a classical IPC question. 这是一个经典的IPC问题。 Because you have run those script between two Machine, the Socket should be a good option. 因为您已经在两台计算机之间运行了这些脚本,所以Socket应该是一个不错的选择。 and you can run method1 and then send something to the PC2 by socket , to do that your method2 have to register in poll. 然后您可以运行method1 ,然后通过socketPC2发送一些内容,以使method2必须在poll中进行注册。 like epoll . 就像epoll一样。 There are also many good blogs show you how to use it by Python , like this . 也有很多不错的博客向您展示如何通过Python使用它,就像这样 If you know good at event driver mode , the multi-thread mode is another good option to wait the trigger occur. 如果您擅长event driver mode ,那么多线程模式是等待触发器发生的另一种不错的选择。

It seems like you are setting up a message broker / shared task queue between two PC's. 似乎您正在两台PC之间设置消息代理 /共享任务队列。

If you can communicate between PC's using ssh, I'd suggest either setting up a simple HTTP server on each machine and sending a POST message with the data via curl to localhost:8000/:script_name, or communicating with the process via a unix socket . 如果您可以使用ssh在PC之间进行通信,建议您在每台计算机上设置一个简单的HTTP服务器 ,然后通过curl将POST消息与数据一起发送到localhost:8000 /:script_name,或者通过unix套接字与该进程进行通信。

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

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