admin管理员组

文章数量:1122832

I'm having an issue with a Flask application that I'm running using Apache and WSGI on a Raspberry Pi. The application restarts intermittently, and it seems that Apache is restarting the application creating a new process after that. I've tried several solutions, but I haven't been able to identify the exact cause of the behavior. Below are the details of my setup and the observed behavior.

Development Environment:

  1. Hardware: Raspberry Pi Web server: Apache Flask: Running with WSGI

  2. Observed Issue: My Flask application seems to restart (or started twice). I've added logging, and I noticed that when this happens, Apache is creating a new process to run the application. This causes problems, especially because I'm managing IPC communication between Python and another C process using FIFOs. Every time Apache creates a new process, my FIFOs get reset, and the data becomes corrupted.

  3. Log Details Suggesting the Issue: Reviewing the logs, I see that after the following log entry: [11-21 04:13:24] p1444 '''DEBUG - Waiting for a message to be available in the outgoing queue...''' Immediately afterward, a new process seems to be created that re-executes flask_buttons.py, which is my main Flask file. I suspect this might be related to the WSGI configuration or an Apache configuration issue.

  4. Apache Configuration File (configuration): This is my Apache config file:(see picture)

Timeout 600

<virtualHost:80>
    ServerName IPAddress
    WSGIDaemonProcess blackbox_flaskapp user=www-data group=www-data threads=5 processes=1  inactivity-timeout=600
    WSGIScriptAlias / /var/www/BlackBox/app.wsgi
    KeepAlive Off
    MaxKeepAliveRequests 5000
    KeepAliveTimeout 1000

    <IfModule mod_headers.c>
        Header always set X-Accel-Buffering "no"
        Header always set Cache-Control "no-cache"
        Header always set Connection "keep-alive"
    </IfModule>

    <Location /events>
        Header set Content-Type "text/event-stream"
        Header set Cache-Control "no-cache"
        Header set Connection "keep-alive"
        Header set Access-Control-Allow-Origin "*"
    </Location>

    <Directory /var/www/BlackBox/FrontMiddleEnd/SFWM_IPC_sse/static>
        Require all granted
    </Directory>

    ErrorLog /var/www/BlackBox/logs/error.log
    CustomLog /var/www/BlackBox/logs/access.log combined
</VirtualHost>

Relevant Python Code Fragment (IPC_CommHandlers.py): Here’s the code where I think the issue might be occurring. I'm using threads to handle incoming and outgoing message queues:

class IPC_Comm_Incoming_Message_Listener():
    MessageQueue = None
    MessageLock = None

    def __init__(self):
        self.MessageQueue = []
        self.MessageLock = Condition()
    
    def getMessage(self):
        with self.MessageLock:
            LoggerTool.instance().debug("Waiting for a message to be available in the queue...")
            self.MessageLock.wait_for(lambda: len(self.MessageQueue) > 0)
            LoggerTool.instance().debug(f"Message received. Queue size before pop: {len(self.MessageQueue)}")
            return self.MessageQueue.pop()
    
    def addMessage(self, message):
        with self.MessageLock:
            LoggerTool.instance().debug(f"Adding message to queue: {message}")
            self.MessageQueue.append(message)
            LoggerTool.instance().debug(f"Message added to queue. Queue size after addition: {len(self.MessageQueue)}")
            self.MessageLock.notify() # Notifies other thread

Any suggestions or advice would be greatly appreciated. Thanks in advance!

I've tried several solutions, but I haven't been able to identify the exact cause of the behavior. Since I am using SSE (Server sent events), I added the following section to the conf file but I am not sure if that is the correct way to do it: <Location /events> Header set Content-Type "text/event-stream" Header set Cache-Control "no-cache" Header set Connection "keep-alive" Header set Access-Control-Allow-Origin "*"

本文标签: Flask App Randomly Restarting When Running with Apache and modwsgiStack Overflow