Fabric in Django .How to solve view waiting for SSH to complete

此生再无相见时 提交于 2019-12-07 16:36:15

问题


I have a simple view:

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

In fabfile.py:

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

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?


回答1:


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.

Task queues

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.

* A task queue like Celery might look complicated to setup but it's not, and has solid support for use from within Django too.




回答2:


for simple cases you can use Threads, however you have to deal with failures after delivering the view. That might be a bit tricky, are you interested in the return value at all?

def ssh_connect():
 os.system('fab remote_uname -i /path/to/keyfile -H hostname')    

def test(request):
 threading.Thread(target=ssh_connect, args=()).start()
 return HttpResposnse('Complete')


来源:https://stackoverflow.com/questions/34542679/fabric-in-django-how-to-solve-view-waiting-for-ssh-to-complete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!