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

  • SEARCH
  • Home
  • 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 4271570
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:27:35+00:00 2026-05-21T07:27:35+00:00

I’m testing out an interesting threading concept in PHP. Basically, I’m working to have

  • 0

I’m testing out an interesting “threading” concept in PHP. Basically, I’m working to have a parent process spawn children that do stuff and return the results back to the parent via STDOUT in sort of a callback manner.

The way my example does it, the inner loop is too tight and burns 99% CPU:

while(true){
        $ret = $thread->Listen();
        if( $ret == 'DOINGSTUFF' ){
                break;
        }
}

I’d like to tone this while loop down to looping every 5 seconds using, for example, a sleep(). This brings CPU utilization to a much more rational level; however I’m concerned it will then “miss” some of the children “threads” returning results in case one child finishes during the parent’s sleeping process.

Can a PHP expert tell me whether this concern is well founded and if so, whether there’s anything I can do to cope?

  • 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-21T07:27:36+00:00Added an answer on May 21, 2026 at 7:27 am

    Your example uses proc_open, which isn’t exactly multi-threading. Anyway, when using proc_open, the pipes streams will be available as long as proc_close is not called, even if the process has actually terminated:

    $proc = proc_open('which ls', $descriptorspec, $pipes);
    sleep(5);
    echo stream_get_contents($pipes[1]); // output '/bin/ls'
    
    $proc = proc_open('which ls', $descriptorspec, $pipes);
    sleep(5);
    proc_close($proc);
    echo stream_get_contents($pipes[1]); // E_WARNING: $pipes[1] not a valid stream
    

    Also, it is not necessary to use sleep. Usually, you don’t want a script hanging for 5 seconds every pass doing nothing. Use usleep instead, which will also reduce the CPU usage but will still make the script responsive:

    while (true) {
       // do stuff
       usleep(50);
    }
    

    The above will sleep for 50 micro-seconds, leaving CPU cycles to other running processes.

    As for pure multi-threading, it is not possible in PHP. All you can do is fork a process. The difference with actual forking and the method you use (proc_open) is that parent process data (variables, etc.) are forked to the child process as well. Forking should not be used in web applications, but only command-line programs, because web servers do not support it very well.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.