Fabric在Django。如何解决等待SSH完成的视图 [英] Fabric in Django .How to solve view waiting for SSH to complete

查看:175
本文介绍了Fabric在Django。如何解决等待SSH完成的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的视图:

def test(request):
 os.system('fab remote_uname -i /path/to/keyfile -H hostname')
 return HttpResposnse('Complete')

fabfile.py 中:

from fabric.api import *
env.user='ubuntu'
def remote_uname():
 run('uname -a')

当我点击url等待很长时间来传递响应。这可能会使用户再次点击刷新执行 remote_uname()。如何交付视图,让操作在以后完成?
如何在这种情况下实现并发?

When I hit the url it waits long to deliver the response. This may confuse user to hit refresh executing remote_uname() again. How can I deliver the view and let the operations complete later? How can I implement concurrency in this case?

推荐答案

线程/进程并发可能需要执行



您有几个选项,如线程/进程级别的并发性,例如使用Python的 多处理 模块。但是走这条路线,我恐怕你可能会暴露出比你的硬件可以处理更多的线程/进程,除非你调节Django视图或限制速度。

Threads/processes concurrency might need throtlling

You have several options, like concurrency on a thread/process level e.g. with Python's multiprocessing module. But going this route I fear you might be exposed to spawning more threads/processes than your hardware can handle, unless you throttle your Django view or something to limit the rate.

它完成工作,非常简单,您的视图将立即返回,但是您可能会承受重载的风险,并且手动管理结果和错误。

It gets the job done, very simple and your view will return immediately, but you risk being overloaded, and you manage results and errors manually.

否则,您可以将其作为排队的任务实现,例如芹菜 *例如,
将排队这些任务,一个一个地运行它们,或者在一个并发级别上运行它们。另一个加号是成功的结果,并且记录失败,所以如果你愿意,你可以将它们返回给用户。

Otherwise, you can implement this as a queued task, like Celery* e.g., which will queue up the tasks, run them in one by one or at a concurrency level you set. Another plus is the result in case of successs, and failures are logged, so you can return them back to the user if you wish to.

*任务排队像Celery可能看起来很复杂的设置,但它不是,并有坚定的支持,从Django中使用。

这篇关于Fabric在Django。如何解决等待SSH完成的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆