Z
Z
Zagir Majidov2022-03-14 20:08:00
Python
Zagir Majidov, 2022-03-14 20:08:00

Why is an error thrown in Thread Python?

I have a function like this:

def timeout(data, new_value, wait_time):
  print('Start_timeout')
  time.sleep(wait_time)
  print(session.get(data))
  session[data] = new_value
  print('End_timeout')

  flash("Извините, срок действия активации истёк!", "danger")
  return redirect("/admin/sing")

And I call this function via:
threading.Thread(target=timeout, args=('admin_sing', False, 5), daemon=True).start()

Error called after time.sleep():
Exception in thread Thread-6:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/home/xpeawey/Рабочий стол/docs/AnyFace/app.py", line 43, in run
    session[self.data] = self.new_value
  File "/home/xpeawey/.local/lib/python3.8/site-packages/werkzeug/local.py", line 436, in __get__
    obj = instance._get_current_object()
  File "/home/xpeawey/.local/lib/python3.8/site-packages/werkzeug/local.py", line 565, in _get_current_object
    return self.__local()  # type: ignore
  File "/home/xpeawey/.local/lib/python3.8/site-packages/flask/globals.py", line 38, in _lookup_req_object
    raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request.  Consult the documentation on testing for
information about how to avoid this problem.

How to fix this error?

SOLUTION:
Python file:
session['admin_timeout'] = 1000 * 60 *60 # Это сессия для скрипта в JavaScript

JavaScript file:
$(document).ready(function() {
   timeout = '{{ session.get('admin_timeout') }}'
   setTimeout(function() {
      window.location.replace('/admin/sing') // Перенаправляем пользователя на вход в админ панель
}, timeout) // Что делает эта функция: Мы берём из сесии 'admin_timeout' время по окончанию которого админ должен авторизоваться, это дополнительная защита для сайта(или же я просто тупой..)!
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2022-03-14
@Zagir-vip

Not everything can be used in a multi-threaded environment. Flask is not thread safe .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question