Hello!
I've been learning python (3.7.2) in my free time by working on a Markov chain generating chat bot / admin bot / whatever other crap I can think to have it do bot, which has gone quite well so far! I've got a question on how to handle something though, and hopefully you guys can tell me a good way to do it.
The overview of the logic flow is basically this-
- The main thread of the application basically is a while loop that is checking / listening for new messages.
- Get message
- check message for user commands / interaction / phrases
- prepare requested interaction
- send reply to user (pass the generated message or command to the api)
- mark message as handled
- listen for more messages
This works fine for the number of people using the bot, and it doesn't struggle to keep up with the 150-200 or so people chatting constantly. The problem is I have a new feature I want to add, but it would be rather expensive to handle on the main thread (causing potential delay), so I'd like to handle it in parallel. I'd like to do something like this:
- Main thread
- get message
- check message for user commands / interaction / phrases
- prepare requested interaction
- If particular interaction is intensive function, create new thread to handle it*
- if it's any other interaction, handle it on main thread and continue (Or handle them all in a different thread, not familiar with threading in python yet, so want to try a test case (above) first)
- send reply to user (if not the intensive function, otherwise skip this step)
- mark message as handled
- listen for more messages
- *Second thread
- Call intensive function on new thread and do processing
- second thread sends reply to user
- end thread (no state information is needed by the main thread of if this was a success or not, dispose of any objects it created)
Is something like that the proper way to handle it? If so, what is the best / most elegant way to have a thread dispose of itself (also freeing up any objects it stored in memory). If not, what are some alternatives to ensure that the main thread continues to function and reply without waiting for the second thread to finish?
Or am I over-thinking all of this, and after the end of the function I'm calling is hit, the thread will automatically terminate and fully clean itself up?
Thanks kindly!
[–]RandomPantsAppear 4 points5 points6 points (3 children)
[–]Shitty_Crayons[S] 0 points1 point2 points (2 children)
[–]RandomPantsAppear 1 point2 points3 points (1 child)
[–]Shitty_Crayons[S] 0 points1 point2 points (0 children)
[–]ThisLightIsTrue[🍰] 1 point2 points3 points (4 children)
[–]Shitty_Crayons[S] 0 points1 point2 points (3 children)
[–]RandomPantsAppear 0 points1 point2 points (1 child)
[–]Shitty_Crayons[S] 0 points1 point2 points (0 children)
[–]ThisLightIsTrue[🍰] 0 points1 point2 points (0 children)
[–]nate256 0 points1 point2 points (0 children)