Forking in PHP on Windows

后端 未结 4 889
既然无缘
既然无缘 2020-12-13 16:00

We are running PHP on a Windows server (a source of many problems indeed, but migrating is not an option currently). There are a few points where a user-initiated action wi

4条回答
  •  Happy的楠姐
    2020-12-13 16:15

    You can create a daemon/background process to run the code (e.g. sending emails) and the request would just have to add items to the queue, let the deamon do the heavy lifting.

    For example, a file send_emails.bat:

    cls
    C:\PHP533\php.exe D:\web\server.php
    exit
    

    open windows task scheduler, and have the above send_emails.bat run every 30 minutes. Make sure only one instance runs at a time or you might run each task in multiples, or send each email twice. I say 30 minutes in case something breaks temporarily (memory issues, database unavailable, etc), it will re-start every 30 minutes rather than having a never ending process that just stops. The following is a skeleton daemon... not complete or tested I am just typing out an example:

    query('select subject, body, to_email FROM email_queue');
        if($result->num_rows == 0)
        {
            sleep(10); // so we are not taxing the database 
        }
        else
        {
            while($row = $result->fetch_assoc())
            {
            // send email
            }
        }
    }
    exit;
    ?>
    

    Finally you just need the request to add the item to the queue in a database, and let the daemon handle the heavy lifting.

    $db->query('insert into email_queue(to,subject,body) values ('customer@email.com','important email','html body!');
    

提交回复
热议问题