[英]Python script trigger a method in another script
我有两个脚本,说PC1上的script1.py和PC2上的script2.py。 现在,script1.py具有两种方法,如下所示。 必须先完成PC1上的method1()才能执行PC2上的method2()。 那么,如何在已经运行的PC2上触发script2.py的method2()?
我可以将script2.py拆分为两个不同的.py文件,即PC2上的method1.py和method2.py,并在PC1上的script1_method2()完成后调用method2.py,但是然后我需要将foo对象传递给它。 也许我可以腌制foo对象并从文件中读取它?
有没有更好的方法来解决这个问题?
注意:我可以使用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
如果可以使用script2.py(例如,通过sshfs)挂载PC2目录,则可以将此目录添加到PYTHONPATH中,然后编写一个简单程序:
from script1 import *
from script2 import *
script1_method1()
script2_method1()
script1_method2()
script2_method2()
似乎您正在两台PC之间设置消息代理 /共享任务队列。
如果您可以使用ssh在PC之间进行通信,建议您在每台计算机上设置一个简单的HTTP服务器 ,然后通过curl将POST消息与数据一起发送到localhost:8000 /:script_name,或者通过unix套接字与该进程进行通信。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.