从.NET在一个循环中调用Python是太慢太慢、NET、Python

2023-09-08 08:39:00 作者:思念是毒°

我们有一个数字,需要从.NET中的一些循环,只是速度太慢内部调用的Python库。我们使用的Process.Start,这大约需要1秒每次通话,这意味着几个对话框需要30秒加载的1/3(8核的机器上 - 我们的客户很可能会慢得多的计算机)。

We've got a number of python libraries that need to be called from inside some loops in .NET that are just too slow. We're using Process.Start, which is taking about 1/3 of a second per call, which means that a few dialogs take 30 seconds to load (on an 8 core machine - our customers will most likely have much slower computers).

由于种种原因,我们不能使用CSV使用IronPython的(如某些文件的模块已已知的问题IronPython的)。

For various reasons, we can't use IronPython (such as some of the files using the csv module which has known issues with IronPython).

我能做些什么,以加快速度?虽然我是一个Python福利局,其中的一些功能简单的如果elif的其他块,而一些分析显示主要的成本正在启动 python.exe 几十倍。有一些秘密的选项,以启动一个Python进程和流媒体的东西,出来?

What can I do to speed things up? Even though I'm a python newb, some of these functions are simple if elif else blocks, and some profiling shows the main "cost" is starting python.exe a few dozen times. Is there some secret options to starting a single python process and streaming things in and out?

一个类似的问题是这一个。

推荐答案

您可以重写你的脚本以接受来自标准输入的命令来代替,而不是当他们完成后退出。

You can rewrite your scripts to accept commands from the standard input instead and not exit when they're finished.

然后启动一个程序(或它们的池)和饲料,而不是开始的新工艺它们的命令。例如,而不是创建一个新的 some_script.py args1 some_script.py args2 ,...只产卵 some_script_wrapper.py 和饲料吧:

Then start up a process (or a pool of them) and feed them commands, instead of starting new processes. For example instead of creating a new some_script.py args1, some_script.py args2, ... only spawn some_script_wrapper.py and feed it:

args1
args2
\end

您可以节省启动时间这种方式,如果需要,甚至可以让多池中。

You'll save the startup time this way and can even enable multiprocessing in a pool if needed.

当然,你必须确保输入/输出数据部分之间完全处理。为了方便,你可以用你的论点/输出到一个已知的结构化格式(JSON),确保你知道邮件大小,甚至只是用魔法标记来找出当你得到一个数据块的结束

Of course you'll have to make sure input / output is processed completely between data parts. To make it easier, you can wrap your arguments / output into a known structured format (json?), make sure you know the "message size", or even just use magic markers to find out when you get the end of a data chunk.