简体   繁体   English

如何从lua内部将运行时变量传递给python脚本?

[英]how to pass a run time variables to python script from inside lua?

I hava a code in lua produce images and other data containers then these data should be used in another python script. 我在lua中有一个代码来生成图像和其他数据容器,然后这些数据应在另一个python脚本中使用。

i tried to use (os.execute(command)) as it shown here https://www.lua.org/pil/22.2.html but i don't know how to pass these parameters direct from lua to python scriprt as arguments 我试图使用(os.execute(command))就像这里显示的https://www.lua.org/pil/22.2.html但我不知道如何将这些参数直接从lua传递给python scriprt作为参数

Command-line arguments can only be strings—and only relatively short strings, and only strings which can be handled by your default system encoding. 命令行参数只能是字符串,并且只能是较短的字符串,并且只能是默认系统编码可以处理的字符串。

If you need to pass data that's large, or structured, or just arbitrary binary bytes, you can't use command-line arguments. 如果您需要传递较大,结构化或任意二进制字节的数据,则不能使用命令行参数。

The simplest solution is to save the data in a file, then pass the filename as a command-line argument. 最简单的解决方案是将数据保存在文件中,然后将文件名作为命令行参数传递。 Then, on the Python side, you just open and read the same file. 然后,在Python端,您只需打开并读取相同的文件。

You can also stream data into the child process's standard input. 您还可以将数据流式传输到子进程的标准输入中。 Lua doesn't come with anything like Python's subprocess module, but sometimes popen is sufficient. Lua没有像Python的subprocess popen模块那样提供任何东西,但是有时popen就足够了。

When popen isn't sufficient, you can build it yourself atop POSIX or Windows API calls. 如果popen不够用,您可以自己在POSIX或Windows API调用上构建它。 See this answer for an example of doing it for POSIX. 有关针对POSIX进行此操作的示例,请参见此答案

You can also just open a pipe, shared memory segment, etc. for the child to inherit, and then pass a file descriptor or key as a command-line argument. 您也可以打开管道,共享内存段等,以使子代继承,然后将文件描述符或键作为命令行参数传递。

Finally, you can write a simple socket server (possibly using a higher-level protocol that libraries already exist for ranging from Netstrings over UDP up to HTTP) and pass the port number as a command-line argument. 最后,您可以编写一个简单的套接字服务器(可能使用已经存在的高层协议库,范围从UDP上的Netstrings到HTTP),并将端口号作为命令行参数传递。

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

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