Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3346162
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:16:16+00:00 2026-05-18T01:16:16+00:00

Say you were writing a daemon that services a job queue. Various other software

  • 0

Say you were writing a daemon that services a job queue. Various other software writes jobs for the daemon into the queue. The daemon polls the queue every few seconds for pending jobs. Assume the queue is implemented as a table in a MySQL database and that the daemon is a simple loop:

  1. get all due jobs from the queue
  2. do the jobs
  3. sleep for N seconds
  4. goto 1

The daemon must survive interrupted service from the MySQL DB server and disruption to DB connections.

Would you design the daemon to connect to the DB server once per cycle? i.e. connect before 1. and disconnect between 2 an 3?

Or would you have daemon keep a connection open? In which case it needs also to a) detect when the server or connection is not working, b) disconnect and reconnect, and c) do so without accumulating DB connections, dud connection descriptors or other dead resources.

If you have a preference, why?

Pros and cons?

Factors that enter into the design?

Any other approaches?

The answer here: mysql connection from daemon written in php doesn’t say why it’s better to keep the connection open. I read elsewhere that the per-connection overhead in MySQL is very light. So it isn’t obvious why permanently consuming one server connection is better than connecting/disconnecting every few seconds.

In my case the daemon is written in PHP.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:16:16+00:00Added an answer on May 18, 2026 at 1:16 am

    I’m actually work on something very close to what you described, but in my case the daemon doesn’t poll for event it get’s them asynchronously via XMPP (but that’s besides the point).

    Cut out the Middle Man

    I think instead of storing the events in the database and polling them w/ MySQL you could probably use Gearman to send them from the client asynchronously (example).

    Garbage Collection

    PHP isn’t really designed to run as a daemon, and it wasn’t until PHP 5.3 when it got circular reference garbage collection that it became a viable option. It’s very important that you use PHP 5.3 if you want any chance at long term running without memory leaks.

    Another thing to keep in mind about GC is that memory is only free if it’s not longer referenced (anywhere). So if you assign a variable to the global scope, it’ll be there until the daemon exits. It’s important that any code you create or use doesn’t built up variables in places (ie, static log, not removing old data, etc).

    Stat Cache

    Another thing is that it’s important to run clearstatcache every so often. Since your PHP process isn’t restarted it’s important to do this call manually to prevent getting old stat data (which may or may not effect you). According to the documentation these functions are cached.

    Affected functions include stat(), lstat(), file_exists(), is_writable(), is_readable(), is_executable(), is_file(), is_dir(), is_link(), filectime(), fileatime(), filemtime(), fileinode(), filegroup(), fileowner(), filesize(), filetype(), and fileperms().

    Resource management

    If your going to be using thing like MySQL during the lifetime of your process, I’d suggest making one connection at startup and keeping it alive. Even though it might use more ram on the MySQL side, you’ll cut out some latency and CPU overhead by not having to connect every 1 second.

    No URL request

    This may seem obvious, but with CLI PHP there is no URL request info. Some libraries aren’t written with this in mind, and this can cause some problems.

    LooPHP

    I’m going to pop a shameless plug in here for a framework I wrote to help with the management of PHP daemons. LooPHP is a run loop framework that lets you schedule event to happen or create listens for abstract sources (socket, stream, etc). In my case, I have the daemon doing more than 1 thing, so it’s very helpful to have system keep track of all the timers for me so that I can effectively poll stream_select for the XMPP connection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say you are writing an application that must implement the HTTP protocol. Protocols are
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Say for instance I was writing a function that was designed to accept multiple
Let's say that I'm writing a function to convert between temperature scales. I want
Let's say that I'm writing a library in C# and I don't know who
Say I'm writing some ruby code and I want to use the standard Date
This is all hypothetical, so please bear with me. Say I'm writing a tool
Say you have an application divided into 3-tiers: GUI, business logic, and data access.
Say we have the following method: private MyObject foo = new MyObject(); // and
Say I have the following file structure: app/ app.py controllers/ __init__.py project.py plugin.py If

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.