From 01a321c202a1a83550b2bc3b8c50c513f3308d55 Mon Sep 17 00:00:00 2001 From: Igor Chubin Date: Thu, 7 May 2020 23:37:21 +0200 Subject: [PATCH] png rendering in a separate thread --- lib/wttr_srv.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/wttr_srv.py b/lib/wttr_srv.py index 3b2f89a..ca42f90 100644 --- a/lib/wttr_srv.py +++ b/lib/wttr_srv.py @@ -9,6 +9,7 @@ import logging import io import os import time +from gevent.threadpool import ThreadPool from flask import render_template, send_file, make_response import fmt.png @@ -36,6 +37,8 @@ logging.basicConfig(filename=LOG_FILE, level=logging.INFO, format='%(asctime)s % LIMITS = Limits(whitelist=[MY_EXTERNAL_IP], limits=QUERY_LIMITS) +TASKS = ThreadPool(25) + def show_text_file(name, lang): """ show static file `name` for `lang` @@ -212,8 +215,14 @@ def _response(parsed_query, query, fast_mode=False): output = get_wetter(parsed_query) if parsed_query.get('png_filename'): - output = fmt.png.render_ansi( - output, options=parsed_query) + # originally it was just a usual function call, + # but it was a blocking call, so it was moved + # to separate threads: + # + # output = fmt.png.render_ansi( + # output, options=parsed_query) + result = TASKS.spawn(fmt.png.render_ansi, output, options=parsed_query) + output = result.get() else: if query.get('days', '3') != '0' \ and not query.get('no-follow-line') \