I am new to python, also celery I started to use today only, I am trying to read an updating file periodically I have used these: Reading from a frequently updated file, Python - Reading from a text file that is being written in Windows and Read from a log file as it's being written using python but they didn't even read file let alone periodically!!
My view is:
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)
Above configAndRun
method use subprocess to run a long task which create a file, you can imagine it as ping google
where all output goes to a file.
Next method readFile
read that file and display output. Its a celery tasks.py as follows:
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
My form is:
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'}))
my celery.py is:
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))
When I run this project I get something like 5997dad7-c1b9-4258-8f4d-8e45ebcf1c78 as an output on my AlgoResult form.
Can you please give me directions. A code will be appreciated