我正在部署一个 Django 应用程序,我正在使用 apache 进行部署,并将模块 WGSI 和 mongodb 作为数据库。
我每 10 分钟调用一次多个 cronjob,为许多 API 收集数据(使用 django-kronos)。
当服务器启动时,一切都很好,但几个小时后服务器开始滞后,现在他宕机了。使用了 100% 的 CPU。 Here :
这是我的 crontask 之一:
@kronos.register('*/10+2 * * * *')
def update_mongo():
# init the instagram api
api = InstagramAPI(client_id = 'SOCIAL_AUTH_INSTAGRAM_KEY',
client_secret = 'SOCIAL_AUTH_INSTAGRAM_SECRET')
# Get Last post added in database
try:
last_id = Post.objects(keywords = 'news').order_by('-created_at').limit(1)[0].insta_id
except:
last_id = 0
# Get the last Media tagged news
if last_id != 0:
results = api.tag_recent_media(tag_name = 'news', min_tag_id = last_id)
else:
results = api.tag_recent_media(tag_name = 'news')
for result in results:
# if post doesn't exist, we create a new one
try:
post = Post.objects.get(insta_id = result.id)
except:
post = InstaPost()
# Init the post and save it in database
post.save(result)
现在的问题是,如果我不知道问题到底出在哪里,是 apache + django 吗?或者这一行 post = Post.objects.get(insta_id = result.id)
?
编辑
我删除了 cronjob 并遇到了同样的问题,一段时间后物理内存和 CPU 刚刚爆炸并且服务器开始非常滞后。
服务器是临时服务器,非常糟糕:1 个核心,992 MB RAM
最佳答案
您的图表显示了应用程序变慢前后的内存、磁盘和 CPU 事件。这可能是因为您的应用程序内部消耗了内存。当应用程序达到其内存限制时,它将开始交换到磁盘。您能否也发布交换文件使用情况图表?
关于django - 服务器滞后 - Django + mongodb + cronjob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31397590/