简体   繁体   English

有没有办法在不使用子进程的情况下在 python 中使用 Protobuf 编译器?

[英]Is there any way to use Protobuf compiler in python without using subprocess?

I'm writing a script that should run over a path I get as an argument, and compile all .proto files one by one.我正在编写一个脚本,该脚本应该在我作为参数获得的路径上运行,并逐个编译所有 .proto 文件。 Currently I'm using Popen to do that, but it makes sense that we could use the protobuf compiler from within python without using subprocesses.目前我正在使用 Popen 来做到这一点,但我们可以在 python 中使用 protobuf 编译器而不使用子进程是有道理的。 any idea how to do so?知道怎么做吗?

thanks!谢谢!

this is my current code:这是我当前的代码:

def dir_path(string):
    if os.path.isdir(string):
        return string
    else:
        raise NotADirectoryError(string)


def compProtos(frompath, topath):
    for dirpath, dirs, files in os.walk(frompath):
        for filename in files:
            if filename.endswith('.proto'):
                print(
                    f'Currently generating file: {filename}, from path: {dirpath}')
                process = Popen(
                    f'python -m grpc_tools.protoc -I {dirpath} --python_out={topath} --grpc_python_out={topath} {dirpath}/{filename}')


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Run gRPC on protobuffs')
    parser.add_argument('-from', dest="frompath", type=dir_path,
                        required=True, help="Enter protobuffs path", metavar="FILE")
    parser.add_argument('-to', dest="topath", type=dir_path, default=".",
                        help="Enter saving path", metavar="FILE")
    args = parser.parse_args()
    compProtos(args.frompath, args.topath)
    print("Done generating protobuffs, results in folder: " + args.topath)

The grpcio_tools package has protoc available as a function: grpcio_tools包有 protoc 作为函数:

import grpc_tools.protoc
grpc_tools.protoc.main(['protoc', '-I.', '--python_out=.', 'foo.proto'])

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

相关问题 使用子进程模块在Python中执行管道命令的任何方法,而不使用shell = True? - Any way to execute a piped command in Python using subprocess module, without using shell=True? 在python子进程check_output中使用绝对路径的任何方法 - any way to use absolute path in python subprocess check_output 使用Python子进程命令调用C编译器 - Invoking C compiler using Python subprocess command 从Shell脚本捕获值并在Python中使用它,而无需使用子进程 - Capture value from shell script and use it in Python without using subprocess 在python文件中使用google.protobuf.Any - Using google.protobuf.Any in python file 在python中使用google的protobuf而不安装它 - Using google's protobuf in python without installing it "有没有办法在 Windows 上使用 gradle 编译 python protobuf?" - is there a way to compile python protobuf using gradle on windows? 有没有一种方法可以使用python子进程模块的Popen.communicate()而无需等待进程退出? - Is there a way to use the python subprocess module's Popen.communicate() without waiting for the process to exit? 有什么方法可以在不使用 Python 中的任何字符串或数字的情况下打印某些内容? - Is there any way to print something without using any strings or numbers in Python? Python:使用子流程流传输数据而没有死锁? - Python: using subprocess to stream data without deadlocks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM