django休息单线程/阻塞视图

use*_*987 1 python django pyinstaller python-3.x django-rest-framework

在单个用户执行视图时,是否可以阻止访问视图的所有用户执行视图中的代码?一种单线程视图.

我需要它,因为我pyinstaller在此视图中生成python可执行文件,并通过配置文件将用户名传递给可执行文件.

例如:

class CliConfig(APIView):

    def get(self, request, format=None):
        try:
            config['DEFAULT']['username'] = request.user

            #make a build with pyinstaller
            bin_file = open(*generated filepath *, 'rb')
            response = Response(FileWrapper(bin_file), content_type='application/octet-stream')
            response['Content-Disposition'] = 'attachment; filename="%s"' % '*filename*'
            return response
        finally:
            config['DEFAULT']['username'] = ''
Run Code Online (Sandbox Code Playgroud)

所以,基本上我想要的是生成一个python可执行文件,它将在django rest framwork中设置一个唯一的用户名APIView.除了通过设置文件传递用户名之外,我没有看到其他方法.如果有办法 - 会赞赏建议.

python 3.6.5,djangorestframework==3.8.2,pyinstaller==3.3.1

wow*_*in2 5

为什么要在配置中存储用户名?这一代不应该是每个用户吗?

无论如何,在视图中执行耗时的任务并不是一种好习惯.
Celery用于生成可执行文件的长期任务,并在不停止Django的情况下接受任何变量.在此任务结束时,Celery可以将可执行文件发送到电子邮件或其他内容.

from celery import Celery

app = Celery('hello', broker='amqp://guest@localhost//')


@app.task
def generate_executable(username):
    # make a build with pyinstaller with username

    bin_file = open(*generated filepath *, 'rb')
    response = Response(FileWrapper(bin_file), content_type='application/octet-stream')
    response['Content-Disposition'] = 'attachment; filename="%s"' % '*filename*'

    # send email and/or returns as task result

    return response


class CliConfig(APIView):

    def get(self, request, format=None):
        task = generate_executable(request.user)
        task.delay()

        return Response({"status": "started", "task_id": task.task_id})
Run Code Online (Sandbox Code Playgroud)