Hello to all. There is a web application on flask (Python 3). Post data is sent to it with a request (VK api), the application must process it. The result (what is sent in response to the request, return 'ok', 200) must always be the same. The task is this: Accept the request, send it "further along the code." In parallel with this, make a return or send this message ('ok') and status (200) to the client (VC), if this is not done, it will begin to flood with requests that lead to an error. While there is an idea about multithreading:

@app.route('/', methods = ['POST']) def mpage(): decoded = request.get_json() text = obj["body"] inp = list(text.split(sep=' ')) thread = Thread(target=seps, args=inp) thread.start() thread.join() return 'ok',200 def task(inp): #Задача 

But at startup, an error appears:

  self._target(*self._args, **self._kwargs) TypeError: task() takes 1 positional argument but 2 were given 

When the error began to appear, I realized that something was wrong. In general, please help with the problem (Do not solve the error, although if that is the only correct solution to the problem, then it will also work)

  • In an amicable way, such things should be done like this: 1) Flask is launched using a third-party multi-threaded server (uwsgi or gunicorn); 2) After receiving the request, it is sent to the task queue, separate from the flagship (based on RabbitMQ, for example), and returns to VKontakte 200 ok and does nothing more; 3) A completely separate process, not connected in any way with the flag and the web, takes it from this queue and performs the actions you need. And it’s better not to start threads in the web server, in general, this is not the case - andreymal
  • Try to limit yourself to one problem per question. First resolve TypeError , then solve problems further, for example thread.join() blocks the current thread. To create for each request, the new thread is most likely not needed. You can try a pool of threads to use pool.submit(func, *args) , where the pool is created only once when starting the service pool = ThreadPoolExecutor(max_workers=20) (if you start using gunicorn, you can create a pool on_starting ). Use the simplest solution that works. In a more general case, see Celery . - jfs

1 answer 1

Change:

  thread = Thread(target=seps, args=inp) 

on:

  thread = Thread(target=seps, args=(inp,)) 
  • How about the whole? Will the method work normally? - vladF
  • IMHO, it’s better to do as suggested by @andreymal - Narnik Gamarnik