简体   繁体   English

如何使用python执行erlang命令

[英]how to execute erlang command using python

I want to execute some erlang commands, the shell can be launched by some command : 我想执行一些erlang命令,可以通过一些命令启动shell:

/opt/myserver/bin/myserver remote_console / opt / myserver / bin / myserver remote_console

Above command will launch a shell where I can execute erlang commands. 上面的命令将启动一个shell,我可以在其中执行erlang命令。 I want to do the same thing from my python script. 我想从我的python脚本中做同样的事情。

I tried to do this using subprocess, but that does not seem to have worked. 我试图使用子过程来做到这一点,但这似乎没有用。

import shlex
from subprocess import Popen, PIPE

cmd = '/opt/myserver/bin/myserver remote_console'
proc = Popen(shlex.split(cmd), stdin=PIPE, stdout=PIPE, stderr=STDOUT)
er_cmd = web_api_handler:process_rfid(30001,<<“RC“>>,arg).
out = proc.communicate(input=er_cmd)

Above method executes the erlang command but it fails with some reason. 上面的方法执行erlang命令,但是由于某种原因而失败。 However I'm able to execute the same command when shell is launched without using python subprocess. 但是,我可以在启动shell时执行相同的命令,而无需使用python子进程。

I tried using proc.stdin.write() and then use proc.communicate() as well, that did not work as well. 我尝试使用proc.stdin.write() ,然后也使用proc.communicate() ,但效果不佳。

Erlang is not a scripting language, it was not designed to be executed and just die. Erlang不是一种脚本语言,它并非旨在执行而已而死。 Erlang is for long living task, and in your example, a better way to communicate with your python code is to use all communication facilities delivered with Erlang. Erlang是一项长期的任务,在您的示例中,与python代码进行通信的更好方法是使用Erlang提供的所有通信工具。

Another issue you will encounter with your method is about race condition and probably unordered output from Erlang node. 您的方法将遇到的另一个问题是关于竞争条件以及Erlang节点的无序输出。 Erlang don't guaranty starting order of all processes. Erlang不能保证所有进程的启动顺序。

The best way in this case is to use interoperability tools from Erlang. 在这种情况下,最好的方法是使用Erlang的互操作性工具。 A tutorial is available from Erlang Official Documentation . 可从Erlang官方文档中获得教程。 C Nodes, Drivers and NIFs are written in C but you can easily use Ports to interconnect your python script and your erlang code. C节点,驱动程序和NIF是用C编写的,但是您可以轻松地使用端口将python脚本和erlang代码互连。

Another possibility is to use local unix socket (available since release 20) and use it to communicate with your python script. 另一种可能性是使用本地unix套接字(自版本20起可用)并使用它与python脚本进行通信。

I think you need Erlang Script . 我认为您需要Erlang脚本 But I don't recommend you this. 但是我不建议你这样做。 I think you should do it as Mathieu Kerjouan said. 我认为您应该按照Mathieu Kerjouan所说的去做。

If you insist on doing what your said, please consider solution below: 如果您坚持要做您所说的话,请考虑以下解决方案:

  1. Start your Erlang application as a daemon. 作为守护程序启动您的Erlang应用程序。
  2. Use escript to communicate with your application by Erlang's rpc. 使用escript通过Erlang的rpc与您的应用程序进行通信。
  3. Control the escript in your python code. 在您的python代码中控制escript。

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

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