Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 停止Django中正在运行的函数_Python_Django_Django Views - Fatal编程技术网

Python 停止Django中正在运行的函数

Python 停止Django中正在运行的函数,python,django,django-views,Python,Django,Django Views,我正在上载CSV文件,然后在数据库中添加行,我希望能够暂停、恢复或终止处理上载的功能 因此,当文件仍在将文件上载到服务器或服务器正在将CSV文件的内容添加到数据库时,用户可以暂停或终止 我试图使用线程,但我不能这样做,然后我尝试time.sleep() 但它只是运行,它不会暂停显示功能,当我尝试停止暂停功能时,它不会停止 视图.py is_paused = False def show(request): content = {} if not is_paused:

我正在上载CSV文件,然后在数据库中添加行,我希望能够暂停、恢复或终止处理上载的功能 因此,当文件仍在将文件上载到服务器或服务器正在将CSV文件的内容添加到数据库时,用户可以暂停或终止 我试图使用线程,但我不能这样做,然后我尝试
time.sleep()
但它只是运行,它不会暂停显示功能,当我尝试停止暂停功能时,它不会停止

视图.py

is_paused = False
def show(request):
    content =  {}
    if not is_paused:  
        if request.method =='POST':
            uploaded_file = request.FILES['file']
            print(uploaded_file.name)
            print(uploaded_file.size)
            fs = FileSystemStorage()
            fs.save(uploaded_file.name,uploaded_file)
            upload = Upload()
            upload.title = uploaded_file.name
            upload.file = uploaded_file
            upload.save()
            filecontent = file_content()
            path = '/home/ahmed/Desktop/atlan/atlan/media/'+uploaded_file.name
            with open(path) as csv_file:
            csv_reader = csv.reader(csv_file, delimiter=',')
            next(csv_reader)
            for row in csv_reader:
                    filecontent.seq = row[0]
                    filecontent.First_name =row[1] 
                    filecontent.Last_name = row[2]
                    filecontent.age = row[3]
                    filecontent.street = row[4]
                    filecontent.city = row[5]
                    filecontent.state = row[6]
                    filecontent.zipcode = row[7]
                    filecontent.dollar = row[8]
                    filecontent.color = row[9]
                    filecontent.date = row[10]
                    file_content.CSV_ID = upload.ID
                    filecontent.save()
    else:
        time.sleep(2)
return render(request,'upload/upload.html', content)

def pause_upload(request):
    global is_paused
    if is_paused == True:
        is_paused = False
    else:
        is_paused = True
    return HttpResponse("PAUSED")

def resume(request):
    stop_threads = 0
    t1 = Thread(target = pause_upload, args =(lambda : stop_threads, )) 
    t1.start() 
    time.sleep(1) 
    stop_threads = 1
    t1.join() 
    print('thread continued')
    return render(request,'upload/upload.html')    


def stop(request):
    pause_upload(request,stop=True)
    sys.exit()

    return HttpResponse(request,'stopped')
upload.html

<body>
<div class="container">
    <form method="POST" enctype="multipart/form-data">
        {% csrf_token %}
        <div class="custom-file">
            <label class="text" for="title">title</label>
            <input type="text" class="text" name="title">
        </div>
        <div class="custom-file">
            <label class="file-label" for="file">Browse</label>
            <input type="file" class="file-input" name="file">
        </div>
        <button type="submit" class="btn btn-primary">Submit</button>
    </form>
    <div class="container">
       <a href="{% url 'pause_upload' %}" class="btn btn-danger">Pause</a>
       <a href="{% url 'stop' %}" class="btn btn-danger pull-right">Stop</a>
       <a href="{% url 'resume' %}" class="btn btn-success pull-right">Resume</a>
   </div>
   </div>
 </body>

{%csrf_令牌%}
标题
浏览
提交