首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >django:定期读取更新文件,并以表格形式显示数据

django:定期读取更新文件,并以表格形式显示数据
EN

Stack Overflow用户
提问于 2017-09-24 05:40:15
回答 1查看 332关注 0票数 0

我是python的新手,也是芹菜,我今天才开始使用,我正在尝试定期读取更新文件我已经使用了这些:Reading from a frequently updated filePython - Reading from a text file that is being written in WindowsRead from a log file as it's being written using python,但他们甚至没有读取文件,更不用说定期读取文件了!

我的观点是:

代码语言:javascript
运行
复制
def myView(request):
title = 'Algorithms'
if request.method == 'GET':
    template = 'algoInput.html'
    form = AlgoInputForm()
    context = {'title': title, 'form': form}

if request.method == 'POST':
    form = AlgoInputForm(request.POST, request.FILES)
    if form.is_valid():
        task = configAndRun(form.cleaned_data)
        output = readFile.delay('algorithms/out-file.txt')

        template = 'result.html'
        result = AlgoResult(initial={'outputData': output})
        context = {'title': title, 'form': form, 'result': result}

return render(request, template, context)

上面的configAndRun方法使用子进程来运行一个创建文件的长任务,你可以把它想象成ping google,所有的输出都转到一个文件中。接下来,方法readFile读取该文件并显示输出。它是一个芹菜tasks.py,如下:

代码语言:javascript
运行
复制
from celery import shared_task

@shared_task
def readFile(file):
try:
    file = open(file, "r")
    loglines = follow(file)
    data = []
    for line in loglines:
        data.append(line)
    return data
except Exception as e:
    raise e

def follow(thefile):
    thefile.seek(0,2)
    while True:
        line = thefile.readline()
        if not line:
            time.sleep(0.1)
            continue
        yield line

我的表单是:

代码语言:javascript
运行
复制
class AlgoInputForm(forms.Form):
    epoch = forms.IntegerField(label='Epoch', help_text='Enter Epoch.')
    learnRate = forms.IntegerField(label='Learning rate(η)', help_text='Enter Epoch.')
    miniBatch = forms.IntegerField(label='Mini-batch size(B)', help_text='Enter Mini-batch size.')
class AlgoResult(forms.Form):
    outputData = forms.CharField(label='Evaluation Results', required=False,widget=forms.Textarea(attrs={'rows': 30, 'cols': 80, 'readonly': 'readonly'}))

我的celery.py是:

代码语言:javascript
运行
复制
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'WebApp.settings')

app = Celery('WebApp')

app.config_from_object('django.conf:settings', namespace='CELERY')

app.autodiscover_tasks()

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

当我运行这个项目时,我在AlgoResult表单上得到类似于5997dad7-c1b9-4258-8f4d-8e45ebcf1c78的输出。

你能告诉我怎么走吗?一个代码将会很受欢迎

EN

回答 1

Stack Overflow用户

发布于 2017-09-24 06:30:01

您在表单中看到的实际上是celery task_id

您不能期望静态Django表单异步运行,并自动跟踪您的任务执行和更新前端。

您应该通过实现其中一种技术( WebSockets、长池等)来实现对前端的异步更新。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46384532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档