简体   繁体   English

从Web服务器在AWS上触发Python脚本

[英]Trigger a Python Script on AWS from Web Server

To give a brief idea of my current setup: 简要介绍一下我当前的设置:

HTML form to collect the data for the Python Script HTML表单,用于收集Python脚本的数据

PHP inserts form data (POST) into input MySQL table PHP将表单数据(POST)插入输入的MySQL表中

PHP exec s Python3.4 script.py row_id PHP exec的Python3.4 script.py row_id

Python collected the row ID using sys.argv[1] and runs the code and inserts the results into output MySQL table. Python使用sys.argv[1]收集了行ID,然后运行代码并将结果插入到输出MySQL表中。 When complete PHP displays a report (PHP waits for Python to complete, then I use header to go to the report URL). 完成后,PHP将显示一个报告(PHP等待Python完成),然后使用header转到报告URL。

The report pages are html with the blanks filled by PHP from the output MySQL table. 报告页面为html,空白部分由MySQL从输出MySQL表中填充。

This worked fine for me testing and developing. 这对我的测试和开发工作正常。 The problem came when I released it into the wild for more general testing within the company - it is hosted on a VPS server and when the Server ran more than one instance of Python it ran like a dog (as we say in Scotland, because to say slow would be too easy). 问题出在我将其发布以进行公司内部的更常规测试时-它托管在VPS服务器上,并且当服务器运行多个Python实例时,它像狗一样运行(正如我们在苏格兰所说,因为说慢点太容易了)。

Of course, the hosting company suggest upgrading, which we did and that doubled the capacity (from 1, sometimes 2 simultaneous runs, to 2 sometimes 3). 当然,托管公司建议进行升级,这是我们所做的,并且使容量增加了一倍(从1个,有时同时运行2个,到2个有时3个)。 I have now reached the conclusion that this method is not scalable, we are always going to hit some kind of limit. 我现在得出的结论是这种方法不可扩展,我们总是会遇到某种限制。

So I have been looking at Amazon Web Services, but to do this requires a change in philosophy for the Python design - it must run all the time and have data fed in somehow (as far as I can see). 因此,我一直在研究Amazon Web Services,但是要做到这一点,就需要改变Python设计的理念-它必须一直运行,并且以某种方式馈送数据(据我所知)。

The Amazon suggestion is Flask or similar framework, but this would mean I then either have two web servers and have to do some kind of cross domain transfer of data, or else scrap what I have done - or at least heavily modify my PHP/HTML/JS that runs on the webserver so that it can be served by Flask and all be hosted by AWS. 亚马逊的建议是Flask或类似的框架,但这意味着我要么拥有两台Web服务器,就必须进行某种跨域数据传输,要么废弃我所做的工作-或至少大量修改我的PHP / HTML / JS在网络服务器上运行,以便Flask可以提供服务,而所有服务都可以由AWS托管。

Unless someone can suggest another way of communication that doesn't require a web framework, I have been considering either just polling the MySQL database at intervals and processing any new data, or else migrating the database to Postgre, which, I believe, allows Python code to run within the database structure and would enable me to trigger on insert (I believe the user defined triggers in MySQL only support C/C++). 除非有人可以提出不需要Web框架的另一种通信方式,否则我一直在考虑要么定期轮询MySQL数据库并处理任何新数据,要么将数据库迁移到Postgre,我相信这允许使用Python代码在数据库结构中运行,并使我能够在插入时触发(我相信MySQL中用户定义的触发器仅支持C / C ++)。

Thanks for any suggestions or pointing out of things I am missing. 感谢您的任何建议或指出我所缺少的东西。

Blair 布莱尔

Further to infinigrove's suggestion Pyro4 works well for my use case, there is an excellent tutorial that provides an easy learning curve. 除了infinigrove的建议之外,Pyro4在我的用例中很好用,还有一个出色的教程 ,提供了简单的学习曲线。

Selcuk's suggestion would probably also have worked, but I would still have been left with the problem of running the code on other servers in a distributed environment. Selcuk的建议可能也可行,但我仍然会遇到在分布式环境中的其他服务器上运行代码的问题。 Also if there is a choice between writing Python code and writing PHP I am afraid I take Python every time! 另外,如果在编写Python代码和编写PHP之间可以选择的话,恐怕我每次都会选择Python!

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

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