Answer the question
In order to leave comments, you need to log in
How to build an asynchronous chat bot for VK?
Hello, I started to understand asynchronous programming, watched a video, read articles and decided to make a chat bot for VKontakte. But I don't know how to organize it right now. In the synchronous code, I had the following logic:
There is a Function that establishes a connection with a longpoll server and waits for an event, if an event arrives, then it calls the bot function passing the necessary parameters to it.
Now I'm trying to figure out how best to proceed, I have 2 options.
Option 1:
Make the connection function with the longpoll server asynchronous, add it to tasks several times and run it, it turns out that several connections will be opened (if I understand correctly), and each function will already call the bot function (it is also asynchronous).
Option 2:
Open only 1 connection to the longpoll server, in this connection I will queue all incoming events. Add the bot function several times to the task, and they will constantly check for the presence of events in the queue through while True and then execute the necessary code.
I apologize if I imagine something not at all as it should be, I just started to deal with asynchronous code, I'm trying to get to the bottom of it.
Answer the question
In order to leave comments, you need to log in
First, read about vk callback api. Based on an asynchronous web server like aiohttp or tornado will be very fast.
Secondly, as far as I understand, you wanted to use a list instead of a queue. Not necessary. If to use, then at least queue.Queue.
Thirdly, the first option is clearly a failure, it will not work very cool, even if you make it work somehow. The second option looks more logical if you still decide to use longpull, but here I think you can’t do without multithreading / multiprocessing.
As a result: if you do it through longpull, the easiest option is one process / thread with an infinite loop that does await session.get(longpoll_url).json(), then OR adds tasks to the queue for the handler, which works in the second process with the same endless loop OR spawns a new thread/process for each new message. But here immediately, even before the design stage, many bottlenecks are visible. If you do it through a callback, there are, in fact, no bottlenecks. For example, a multiprocessor asynchronous tornado (but inferior in a single thread to asynchronous aiohttp by default), will cope with any load, there are no bottlenecks, in fact, it will, in fact, rake up new requests in its threads / processes, you will not need to think about it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question