简体   繁体   English

python如何在内部工作? 是不是一个python的解释器可以使CPU使用率太高?

[英]How python works internally? Does more than one interpreter of python can make CPU usage too much high?

Scenario 脚本

I am invoking python scripts using exec(...) in php, and python works its part and it is unstructured environment, there is no django or any other framework. 我在php中使用exec(...)调用python脚本,python工作它的部分,它是非结构化的环境,没有django或任何其他框架。

And a product is client based product & most of the tasks are long running tasks. 产品是基于客户端的产品,大多数任务都是长期运行的任务。

So if there is any request from client, it must be satisfied immediately. 因此,如果客户有任何要求,必须立即满足。 Here for us, time is also a main concern we can not delay the process. 对我们来说,时间也是我们不能耽误过程的主要问题。

So if I will get too much requests from client (May be, crons are also working at the same time), I will get too much python processes and according to my knowledge each exec(...) will invoke interpreter. 因此,如果我从客户端收到太多请求(也许,crons也在同一时间工作),我会得到太多的python进程,据我所知,每个exec(...)都会调用解释器。 So if anyhow I can prevent invoking the interpreter at each call, it will be good for me. 因此,无论如何,我可以阻止在每次通话时调用解释器,这对我有好处。 Memory space of 1Kb or 1Mb is very useful. 1Kb或1Mb的存储空间非常有用。

Problems 问题

  1. Memory usage is high (If I can save 1Mb, I want try any solution. Assuming script is well written. Proper sleep is given in looping and garbage collector is managed well.) 内存使用率很高(如果我可以保存1Mb,我想尝试任何解决方案。假设脚本写得很好。循环中给出了适当的睡眠,垃圾收集器管理得很好。)
  2. Sometimes for long running python script, if that script is invoked from php, using exec(...) and process is not invoked in background , and that python script is over, and still it takes time to execute the next line after that exec. 有时对于长时间运行的python脚本,如果从php调用该脚本,则使用exec(...)并且后台不会调用进程,并且该python脚本已经结束,并且仍需要时间来执行该exec之后的下一行。

Solution

For the solution, I thought I should use API calls for python. 对于解决方案,我认为我应该使用python的API调用。 But remember I can not use any framework. 但请记住,我不能使用任何框架。 To use framework I need to rewrite the code, and that is not possible in my case. 要使用框架,我需要重写代码,这在我的情况下是不可能的。 So CGI and FCGI can help me here. 所以CGI和FCGI可以帮助我。

To run python scripts from API calls, I need to enable CGI to run. 要从API调用运行python脚本,我需要启用CGI才能运行。

But what I found is interesting :: 但我发现有趣的是::

Programs using CGI, to communicate with their web server, need to be started by the server for every request. 使用CGI与其Web服务器通信的程序需要由服务器为每个请求启动。 So, every request starts a new Python interpreter – which takes some time to start up – thus making the whole interface only usable for low load situations. 因此, 每个请求都会启动一个新的Python解释器 - 这需要一些时间来启动 - 从而使整个接口仅适用于低负载情况。

common-gateway-interface . 通用网关接口

If I will run a script like following, than all will start at once 如果我将运行如下的脚本,那么一切都将立即启动

subprocess.Popen(["python", "program1.py"])
subprocess.Popen(["python", "program2.py"])
subprocess.Popen(["python", "program3.py"])
subprocess.Popen(["python", "program4.py"])
subprocess.Popen(["python", "program5.py"])
subprocess.Popen(["python", "program6.py"])

Assume this all programs are long running task. 假设所有程序都是长时间运行的任务。

1) Can I prevent python module from creating more interpreter and if yes how can I? 1)我可以阻止python模块创建更多解释器,如果是,我该怎么办?

2) How many maximum interpreters, a python module can create (at once like the above code) ? 2)python模块可以创建多少个最大解释器(像上面的代码一样)?

I'm not sure if I fully understood what you asked. 我不确定我是否完全明白你的要求。 In generally, in any context, code is executed sequentially unless explicitly told not to do so. 通常,在任何上下文中,代码是顺序执行的,除非明确告知不这样做。 If you put those lines in your shell, your shell first start an instance of python for program1.py . 如果你将这些行放在shell中,你的shell首先会为program1.py启动一个python实例。 You are not able to proceed to execute program2.py until you finish program1.py . 在完成program1.py之前,您无法继续执行program2.py . So you have only one python interpreter on at any time point. 所以你在任何时间点都只有一个python解释器。

If you want to run each script as a background process, you need to put & at the end of each line this python program1.py & . 如果要将每个脚本作为后台进程运行,则需要在每行的末尾放置此python program1.py & In this way you can run several scripts at the same time. 通过这种方式,您可以同时运行多个脚本。 If your scripts take long to finish, you will have 6 interpreters running at the same time. 如果您的脚本需要很长时间才能完成,那么您将同时运行6个解释器。 I'm pretty sure every script needs exactly one interpreter designated to it. 我很确定每个脚本都需要一个指定的解释器。

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

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