简体   繁体   English

将Python脚本部署到Windows和Linux

[英]Deploying a Python Script to Windows and Linux

I have a python server that I need to run in both a Linux and Windows environment, and my question is about deployment. 我有一个在Linux和Windows环境中都需要运行的python服务器,我的问题是关于部署。 What is the best method for deploying the solution instead of just double clicking on the file and running it? 部署解决方案而不是双击文件并运行它的最佳方法是什么?

Since I use the server_forever() on the server, I can just run the script from command line, but this keeps the python window open. 由于我在服务器上使用server_forever(),因此只能从命令行运行脚本,但这会使python窗口保持打开状态。 If I log off the machine, naturally the process will stop. 如果我注销计算机,则该过程自然会停止。 So what is the best method for deploying a python script that needs to keep running if the user is logged in or off a machine. 那么,如果用户登录或退出计算机,部署需要保​​持运行状态的python脚本的最佳方法是什么。

Since I am going to be using multiple environment, Linux and Windows, can you please be specific in what OS you are talking about? 由于我将使用Linux和Windows等多种环境,因此请您能具体说明所使用的OS?

For windows, I was thinking of running the script 'At Startup' using the Windows scheduler. 对于Windows,我正在考虑使用Windows计划程序运行脚本“启动时”。 But I wanted to see if anyone had a better option. 但我想看看是否有人有更好的选择。 For linux, I really don't know what to create. 对于linux,我真的不知道要创建什么。 I am assuming a CRON job? 我正在担任CRON工作?

Deployment does refer to coding, so using serve_forever() on a multiprocessing job manager keeps the python window open upon execution. 部署确实涉及编码,因此在多处理作业管理器上使用serve_forever()可使python窗口在执行时保持打开状态。 Is there a way to hide this window through code? 有没有办法通过代码隐藏此窗口? Would you recommend using a conversion tool like py2exe instead? 您是否建议使用像py2exe这样的转换工具?

This is the subject matter of a whole library of books, so I will just give an introduction here :-) You can basically start scripts directly and then have multiple options to do this in a way that they keep running in the background. 这是整个书籍库的主题,因此我在这里仅作介绍:-)您基本上可以直接启动脚本,然后有多个选项可以使脚本在后台继续运行。

If you have certain functionality that needs to run on regular moments, you would do this by scheduling it: 如果您有某些需要定期运行的功能,则可以通过安排它来实现:

  • Windows: Windows Scheduler or specific scheduling tools Windows:Windows Scheduler或特定的计划工具
  • Linux: Cron Linux: Cron

If your problem is that you want to start a script without it closing on you while SSH'ing into Linux, you want to look into the "screen" or "tmux" tools. 如果您的问题是要在SSH进入Linux时不关闭脚本就启动脚本,则需要查看“屏幕”或“ tmux”工具。

If you want to have it started automatically this could be done by using the "At Startup" as you point out and Linux has similar functionalities, but the preferred and more robust way would be to set up a service that is better integrated with the OS. 如果您希望它自动启动,则可以使用您指出的“启动时”来完成,并且Linux具有类似的功能,但是首选且更可靠的方法是设置与OS更好集成的服务。

Even more capabilities can be yielded by using an application server such a Django 通过使用诸如Django之类的应用服务器,可以产生更多的功能

Tomcat (see comment) is an option, but definitely not the standard one; Tomcat(请参阅注释)是一个选项,但绝对不是标准选项; you'll have a hard time finding support both from Tomcat people running Python or Python people running their stuff on Tomcat. 您将很难从运行Python的Tomcat人员或在Tomcat上运行其东西的Python人员那里获得支持。 That being said, I imagine you could enable CGI and have it run a Python command with your script. 话虽如此,我想您可以启用CGI并使其在脚本中运行Python命令。

Yet, instead of just starting a Python script I would strongly encourage you to have a look at different Python options that are probably available for your specific use case. 但是,我强烈建议您不只是启动Python脚本,而是查看可能适用于您的特定用例的其他Python选项。 From lightweight web solutions like Flask over a versatile networking engine like Twisted to a full blown web framework like Django . 从轻量级的Web解决方案(如Flask)到通用的网络引擎(如Twisted)到功能完善的Web框架(如Django)

They all have rather well-thought-out deployment solutions available. 他们都有可用的经过深思熟虑的部署解决方案。 Look up WSGI for more background. 查找WSGI以获得更多背景信息。

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

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