简体   繁体   English

Python:如何在同一个解释器上运行多个程序

[英]Python: How to Run multiple programs on same interpreter

How to start an always on Python Interpreter on a server? 如何在服务器上启动始终在线的Python解释器?

If bash starts multiple python programs, how can I run it on just one interpreter? 如果bash启动多个python程序,如何在一个解释器上运行它?

And how can I start a new interpreter after tracking number of bash requests, say after X requests to python programs, a new interpreter should start. 以及在跟踪bash请求的数量后如何启动新的解释器,例如在X对python程序的请求之后,应该启动一个新的解释器。

EDIT: Not a copy of https://stackoverflow.com/questions/16372590/should-i-run-1000-python-scripts-at-once?rq=1 编辑:不是https://stackoverflow.com/questions/16372590/should-i-run-1000-python-scripts-at-once?rq=1的副本

Requests may come pouring in sequentially 请求可能会依次涌入

You cannot have new Python programs started through bash run on the same interpreter, each program will always have its own. 您不能在同一个解释器上通过bash启动新的Python程序,每个程序将始终拥有自己的程序。 If you want to limit the number of Python programs running the best approach would be to have a Python daemon process running on your server and instead of creating a new program through bash on each request you would signal the daemon process to create a thread to handle the task. 如果要限制运行的Python程序的数量,最好的方法是在服务器上运行Python守护进程,而不是通过在每个请求上通过bash创建新程序,而向守护进程发出信号以创建要处理的线程任务。

To run a program forever in python: 要在python中永久运行程序:

while True :
     do_work()

You could look at spawning threads for incoming request. 您可以查看生成的传入请求线程。 Look at threading.Thread class. 看一下threading.Thread类。

from threading import Thread 从线程导入线程

task = new Thread(target=do_work, args={})
task.start()

You probably want to take a look at http://docs.python.org/3/library/threading.html and http://docs.python.org/3/library/multiprocessing.html ; 您可能想看看http://docs.python.org/3/library/threading.htmlhttp://docs.python.org/3/library/multiprocessing.html threading would be more lightweight but only allows one thread to execute at a time (meaning it won't take advantage of multicore/hyperthreaded systems), while multiprocessing allows for true simultaneous execution but can be a bit less lightweight than threading if you're on a system that doesn't utilize lightweight subprocesses and may not be as necessary if the threads/processes spend lots of time doing I/O requests. threading将更加轻量级,但是一次只允许一个线程执行(这意味着它不会利用多核/超线程系统),而multiprocessing允许真正的同时执行,但是比起threading ,它的轻量级要少一些。在不使用轻量级子进程的系统上,如果线程/进程花费大量时间执行I / O请求,则可能没有必要。

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

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