Python Forum
How do I properly implement restarting a multithreaded python application?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I properly implement restarting a multithreaded python application?
#1
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
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Solved]Help Displaying Emails properly via Python Extra 5 1,187 Sep-28-2022, 09:28 PM
Last Post: deanhystad
  Multithreaded file transfer from multiple volumes skoobi 2 1,150 Jul-28-2022, 07:52 AM
Last Post: skoobi
  Right way to implement interfaces yossiy123 1 1,281 May-12-2022, 10:31 AM
Last Post: Gribouillis
  Python running only in application Mawixy 2 1,139 Apr-19-2022, 11:38 AM
Last Post: Mawixy
  Skipping line in text without Restarting Loop IdMineThat 4 1,499 Apr-05-2022, 04:23 AM
Last Post: deanhystad
  How to send data from a python application to an external application aditya_rajiv 1 2,189 Jul-26-2021, 06:00 AM
Last Post: ndc85430
  python application and credentials safety concern aster 4 3,540 Mar-06-2021, 06:51 PM
Last Post: snippsat
Question best way to implement algorithm hamidze 3 2,223 Feb-27-2021, 07:10 PM
Last Post: hamidze
  Keep Application running after Python script ends PEGylated_User 0 1,992 Nov-12-2020, 03:27 PM
Last Post: PEGylated_User
  Restarting a script new2codez 0 1,135 Nov-11-2020, 11:02 AM
Last Post: new2codez

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020