简体   繁体   English

将 Python3 项目迁移到 AWS

[英]Migrate Python3 project into AWS

I have been investigating how to migrate my python3 project into AWS but I am unable to find a solution.我一直在研究如何将我的 python3 项目迁移到 AWS,但我找不到解决方案。 My python3 project is quite simple:我的 python3 项目非常简单:

daemon.py -> This is an infinite while True loop. daemon.py -> 这是一个无限的 while True 循环。 It does GETs requests to an API.它对 API 执行 GET 请求。 It is important to make as much requests per sec as possible.每秒发出尽可能多的请求非常重要。 Then it stores it into mysql db.然后将其存储到 mysql db 中。 The request is always the same, GET /api/check_stock.请求始终相同,GET /api/check_stock。

while True:
  r = requests.post(url,json=body, proxies=proxy_dict)
  r_json = json.loads(r.content)
  insert_db(r_json)

ship.py -> Another while True loop looking for changes in the mysql, when detects a change sends a GET request to an API. ship.py -> 另一个寻找 mysql 更改的 while True 循环,当检测到更改时,会向 API 发送 GET 请求。

I found out about lambda but I am not sure if is a good idea to run infinite loop on it as I understand they charge you by the execution time... and this is infinite loop.我发现了有关 lambda 的信息,但我不确定在其上运行无限循环是否是个好主意,因为我知道它们会按执行时间向您收费……这是无限循环。

Thanks in advance.提前致谢。

Whether it's effective for you depends only on how much you value each of those downloads and we don't have enough information to know the answer.它是否对您有效仅取决于您对这些下载的重视程度,而我们没有足够的信息来知道答案。

There's also a question of whether you want to run this all the time, or at some specific times for a specific duration.还有一个问题是您是否要一直运行它,或者在特定时间运行特定持续时间。 In the first case, fargate or ec2 may be better candidates, in the second, it depends how long you run the process for.在第一种情况下,fargate 或 ec2 可能是更好的候选者,在第二种情况下,这取决于您运行该进程的时间。

There are some things you can improve though with the current system - you seem to be waiting for each of the responses which means a lot of idle time.尽管使用当前系统,您可以改进一些事情 - 您似乎正在等待每个响应,这意味着大量空闲时间。 You could improve the throughput with either async or threadpool approaches.您可以使用异步或线程池方法提高吞吐量。 (unless it's really only a single endpoint listing all the items) (除非它真的只是一个列出所有项目的端点)

Another one is that you don't necessarily need to run ship.py as a monitoring loop if the state doesn't change very often.另一个是如果 state 不经常更改,您不一定需要运行ship.py作为监控循环。 If you're already in AWS, you could do the notification as SQS messages instead, which would fire the ship.py process as a lambda - that's actually likely to save you some resources.如果您已经在 AWS 中,则可以将通知作为 SQS 消息进行,这会将ship.py进程作为 lambda 触发 - 这实际上可能会为您节省一些资源。

Ok, found out best approach is to run daemon in EC2 with Aurora and try to migrate ship.py as viraptor said.好的,发现最好的方法是使用 Aurora 在 EC2 中运行守护进程,并尝试像 viraptor 所说的那样迁移 ship.py。

if you do use a lambda serverless python project, you will be to do what you intend but you will billed quite substantially.如果您确实使用了 lambda 无服务器 python 项目,那么您将按照您的意愿行事,但您将收取大量费用。

here are the docs:AWS-Lambda-Python这里是文档:AWS-Lambda-Python

Also use SQS to push data from your script to the database.还可以使用SQS将数据从脚本推送到数据库。

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

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