如何将 python 自身对象传递给另一个 python 脚本
How to pass python self object to another python script
我有一个奇怪的要求,我需要从 python 调用脚本,我正在使用 subprocess 模块,然后从脚本调用原始 class 的函数.我有这样的东西 -
import subprocess
import textwrap
import sys
class caller:
def call_script(self):
script = textwrap.dedent("""\
#! /usr/bin/env python
import caller
print ("Before the callback")
caller().callback()
print ("After the callback")
""")
subprocess.Popen(script, shell=True, executable=sys.executable())
def callback(self):
print("inside the callback")
当然现在我意识到将从脚本调用的回调不是执行脚本的同一对象的方法。有没有什么方法可以将 self 对象传递给脚本,或者有什么其他方法可以获取调用脚本的原始对象的回调方法?
该脚本恰好 运行 在一个完全不同的进程中,您必须设计一种在进程之间进行通信的方法。
(为此你可以使用:本地套接字,可能是 multiprocessing 包,可能是通过子进程的管道) - 没有简单的方法将有意义的对象引用传递到另一个进程。
好吧,因为@knitti 建议脚本 运行 在一个完全不同的过程中,所以我通过信号、文件和全局变量的组合解决了这个问题。不声称这是最优雅的解决方案,但它对我有用 -
import subprocess, textwrap, os, signal
caller_object = None
def signal_handler(signum, frame):
caller_object.callback()
comm_file = "/tmp/somefile"
class caller:
def call_script(self):
signal.signal(signal.SIGALRM, mockproc_callback)
# Need to pass the process id to the script.
with open(comm_file, 'w') as pipe:
pipe.write("{0}".format(os.getpid()))
# Also set the global object so that callback function could be called by handler
global caller_object
caller_object = self
script = textwrap.dedent("""\
#! /usr/bin/env python
import caller, signal, os
with open("{0}", "r") as pipe:
# Read the pid of original process
pid = pipe.read()
print ("Before the callback")
# Although its named kill, os.kill could be used to send all sort of signals to a process
os.kill(int(pid), signal.SIGALRM)
print ("After the callback")
""".format(comm_file))
subprocess.Popen(script, shell=True, executable=sys.executable())
def callback(self):
print("inside the callback")
我相信还有很大的改进空间。
我有一个奇怪的要求,我需要从 python 调用脚本,我正在使用 subprocess 模块,然后从脚本调用原始 class 的函数.我有这样的东西 -
import subprocess
import textwrap
import sys
class caller:
def call_script(self):
script = textwrap.dedent("""\
#! /usr/bin/env python
import caller
print ("Before the callback")
caller().callback()
print ("After the callback")
""")
subprocess.Popen(script, shell=True, executable=sys.executable())
def callback(self):
print("inside the callback")
当然现在我意识到将从脚本调用的回调不是执行脚本的同一对象的方法。有没有什么方法可以将 self 对象传递给脚本,或者有什么其他方法可以获取调用脚本的原始对象的回调方法?
该脚本恰好 运行 在一个完全不同的进程中,您必须设计一种在进程之间进行通信的方法。 (为此你可以使用:本地套接字,可能是 multiprocessing 包,可能是通过子进程的管道) - 没有简单的方法将有意义的对象引用传递到另一个进程。
好吧,因为@knitti 建议脚本 运行 在一个完全不同的过程中,所以我通过信号、文件和全局变量的组合解决了这个问题。不声称这是最优雅的解决方案,但它对我有用 -
import subprocess, textwrap, os, signal
caller_object = None
def signal_handler(signum, frame):
caller_object.callback()
comm_file = "/tmp/somefile"
class caller:
def call_script(self):
signal.signal(signal.SIGALRM, mockproc_callback)
# Need to pass the process id to the script.
with open(comm_file, 'w') as pipe:
pipe.write("{0}".format(os.getpid()))
# Also set the global object so that callback function could be called by handler
global caller_object
caller_object = self
script = textwrap.dedent("""\
#! /usr/bin/env python
import caller, signal, os
with open("{0}", "r") as pipe:
# Read the pid of original process
pid = pipe.read()
print ("Before the callback")
# Although its named kill, os.kill could be used to send all sort of signals to a process
os.kill(int(pid), signal.SIGALRM)
print ("After the callback")
""".format(comm_file))
subprocess.Popen(script, shell=True, executable=sys.executable())
def callback(self):
print("inside the callback")
我相信还有很大的改进空间。