简体   繁体   English

Dockerized Flask App-具有持续运行脚本的Rest API

[英]Dockerized Flask App - Rest API with continually running scripts

I have a dockerized Flask application running locally that consists of several REST API endpoints. 我有一个在本地运行的dockerized Flask应用程序,该应用程序由多个REST API端点组成。 It's all working as expected so that when a GET request is performed on one of the endpoints, data is retrieved from the Postgres database and then displayed to the browser as json. 一切都按预期工作,因此在其中一个端点上执行GET请求时,将从Postgres数据库中检索数据,然后将其显示为json。 Great. 大。 The database so far is just test data and now I need to continually update the database with real data. 到目前为止,数据库只是测试数据,现在我需要不断用真实数据更新数据库。

I have the script that pulls data from the web, and I understand how to add it to the database with post and put requests, but what I don't understand, is how and where to have this script continually running, to where it doesn't interfere with the REST API portion of my server and vice versa, almost as though it's a completely separate entity within the backend. 我有从网络提取数据的脚本,而且我了解了如何使用发布和放置请求将其添加到数据库中,但是我不了解的是如何以及在何处连续运行此脚本,以及在何处连续运行不会干扰我服务器的REST API部分,反之亦然,几乎就像它是后端中一个完全独立的实体一样。

To do this, would I create an entirely new flask app that runs on it's own server and is continually running the script and adding the scraped data to the database so that the other flask app which contains the API endpoints can access it when needed? 为此,我是否要创建一个全新的flask应用程序,该应用程序在其自己的服务器上运行,并不断运行脚本并将已抓取的数据添加到数据库中,以便其他包含API端点的flask应用程序可以在需要时访问它? I feel as though I am way off here, and any input on the best way to move forward is extremely appreciated. 我觉得自己离这里还很遥远,并且对于最好的前进方式的任何投入都深表感谢。 Thank you! 谢谢!

You are not far off at all in my oppinion. 在我看来,您一点也不遥不可及。

I would say, let your API stand on it's own - as the gateway to your database. 我要说的是,让您的API自己站起来-作为数据库的网关。 In it's own container. 在它自己的容器中。

The scraping that you want to do is another process - and you should not mix it into the flask API application. 您要执行的抓取是另一个过程-请勿将其混入flask API应用程序中。 Instead, since you are already in the docker realm here - consider creating another image that will do the scraping for you. 相反,由于您已经在docker领域中,请考虑创建另一个将为您进行抓取的映像。 This can be a bash script, a python app - it's not important. 这可以是bash脚本,python应用程序-这并不重要。 Just as long as that you can keep it as simple as possible. 只要您可以使其尽可能简单即可。

You could even consider if you can make that application/script image in such a way, that you could run multiple of them in parallel. 您甚至可以考虑是否可以以这种方式制作该应用程序/脚本映像,从而可以并行运行其中的多个映像。

Yes, you will have two images to maintain. 是的,您将需要维护两个图像。 But they will each be smaller on their own, and less complex. 但是它们各自将变小,并且变得不那么复杂。 And, if done right - you can scale the activity up if needed. 而且,如果操作正确,则可以根据需要扩大活动范围。

Consider the first two statements of the UNIX philosophy : 考虑一下UNIX哲学的前两个陈述:

  1. Make each program do one thing well. 使每个程序都做好一件事。 To do a new job, build afresh rather than complicate old programs by adding new "features". 要完成一项新工作,请重新构建而不是通过添加新的“功能”使旧程序复杂化。
  2. Expect the output of every program to become the input to another, as yet unknown, program. 期望每个程序的输出都将成为另一个程序(尚不为人所知)的输入。 Don't clutter output with extraneous information. 不要将多余的信息弄乱。 Avoid stringently columnar or binary input formats. 避免严格使用列或二进制输入格式。 Don't insist on interactive input. 不要坚持交互式输入。

Maintainability is king in the game of software development. 可维护性是软件开发游戏中的王者。 Big cluttered projects have a hard time surviving in the long run. 从长远来看,杂乱无章的大型项目很难生存。

After thought: If your project is experimental and you just want to prove some concept - then do that. 经过思考:如果您的项目是实验性的,而您只想证明一些概念,那就这样做。 And don't overthink the design. 并且不要过度思考设计。 Too many projects die from that too! 太多的项目也因此而死!

Those are my thought at least. 至少那些是我的想法。

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

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