Python Forum
How do I properly implement restarting a multithreaded python application? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How do I properly implement restarting a multithreaded python application? (/thread-40352.html)



How do I properly implement restarting a multithreaded python application? - MrFentazis - Jul-15-2023

I have a fairly large application consisting of Flask and special software for a fairly complex system managed by a hand written task scheduler. I want to be able to restart my application from the web interface. I'm using Threading, so it follows that I can't force this part of the application to terminate. Some of the tasks can be long, more than 10 minutes. The application uses external resources such as a serial connection to the microcontroller and using a connection to a USB SDR receiver via SoapySDR. It seems to me that using multiprocessing would be quite difficult, due to the use of shared resources. I have little experience with multithreading/multiprocessing programming....

I don't even know which code snippet makes sense to attach..... Help me please...

I don't have any ideas that I'm sure of


RE: How do I properly implement restarting a multithreaded python application? - JamesSmith - Jul-17-2023

We understand that you are having trouble restarting the Flask application from the web interface, given the complexity of your system and the use of shared resources. As a company specializing in Altamira software development, we can provide a few recommendations to help you achieve your goal.
1. Separate long-term tasks: Identify tasks in your program that take more than 10 minutes to complete. Consider moving these tasks to separate processes to provide better control and flexibility during reboots.
2. Use process-based multiprocessing: Instead of multithreading, consider using Python's multiprocessing module. It allows you to create separate processes that can run in parallel and independently of each other. Each process will have its own memory space, which will help avoid conflicts with shared resources.
3. Implement a task queue: Create a task queue to manage the execution of your long-running tasks. You can use a library like Celery or Redis Queue to manage the scheduling and distribution of tasks across multiple processes. This approach allows you to queue tasks and get the results after they are completed.
4. Smooth shutdown and restart: When you initiate a restart from the web interface, you must gently shut down the Flask application and associated processes. Make sure that all tasks are completed or safely terminated before proceeding with the restart. You can create a special endpoint in your Flask application that triggers the shutdown and restart procedure.
5. Save shared resources: To avoid conflicts with shared resources, such as a serial connection or USB SDR receiver, use proper synchronization mechanisms. Use locks, semaphores, or other thread-safe designs to ensure that only one process has access to these resources at a time.
6. Test and iterate: As you mentioned, you have limited experience with multithreaded/multiprocessor programming, so it's important to test your solution thoroughly. Start with smaller, isolated components and gradually expand your scope once you are confident in your implementation. Keep an eye out for any potential problems or race conditions that may arise.