Fixes database locking on banter

This commit is contained in:
2024-02-13 22:47:28 +01:00
parent dfbdcf1459
commit d77ad8fec9
+7 -3
View File
@@ -12,6 +12,8 @@ from random import choice
dotenv.load_dotenv() dotenv.load_dotenv()
UPDATE_FREQ = 10 # How often to save to database
@shared_task @shared_task
def get_banter(id): def get_banter(id):
track = Track.objects.get(pk=id) track = Track.objects.get(pk=id)
@@ -25,13 +27,13 @@ def get_banter(id):
) )
r.raise_for_status() r.raise_for_status()
token = 0
for line in r.iter_lines(): for line in r.iter_lines():
body = json.loads(line) body = json.loads(line)
response_part = body.get('response', '') response_part = body.get('response', '')
# the response streams one token at a time, print that as we receive it token += 1
print(response_part, end='', flush=True)
track.banter += response_part track.banter += response_part
track.save()
if 'error' in body: if 'error' in body:
raise Exception(body['error']) raise Exception(body['error'])
@@ -40,3 +42,5 @@ def get_banter(id):
track.banter_done = True track.banter_done = True
track.save() track.save()
return body['context'] return body['context']
elif token % UPDATE_FREQ == 0:
track.save()