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 7624143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:53:07+00:00 2026-05-31T04:53:07+00:00

I’m having some difficulties getting the PHP libevent extension to break out of a

  • 0

I’m having some difficulties getting the PHP libevent extension to break out of a loop on a timeout. Here’s what I’ve got so far based on the demos on the PHP.net docs pages:

// From here: http://www.php.net/manual/en/libevent.examples.php

function print_line($fd, $events, $arg) {
    static $max_requests = 0;
    $max_requests++;

    printf("Received event: %s after %s\n%s", implode(getEventFlags($events)), getTimer(), fgets($fd));

    if ($max_requests == 10) {
        // exit loop after 3 writes
        echo " [EXIT]\n";
        event_base_loopexit($arg[1]);
    }
}

// create base and event
$base = event_base_new();
$event = event_new();


getTimer(); // Initialise time

$fd = STDIN;
event_set($event, $fd, EV_READ | EV_PERSIST, "print_line", array($event, $base));
event_base_set($event, $base);
event_add($event, 2000000);
event_base_loop($base);

// extract flags from bitmask
function getEventFlags ($ebm) {
    $expFlags = array('EV_TIMEOUT', 'EV_SIGNAL', 'EV_READ', 'EV_WRITE', 'EV_PERSIST');
    $ret = array();
    foreach ($expFlags as $exf) {
        if ($ebm & constant($exf)) {
            $ret[] = $exf;
        }
    }
    return $ret;
}

// Used to track time!
function getTimer () {
    static $ts;
    if (is_null($ts)) {
        $ts = microtime(true);
        return "Timer initialised";
    }
    $newts = microtime(true);
    $r = sprintf("Delta: %.3f", $newts - $ts);
    $ts = $newts;
    return $r;
}

I can see that the timeout value passed to event_add effects the events passed to print_line(), if these events are any more than 2 seconds apart I get an EV_TIMEOUT instead of an EV_READ. What I want however is for libevent to call print_line as soon as the timeout is reached rather than waiting for the next event in order to give me the timeout.

I’ve tried using event_base_loopexit($base, 2000000), this causes the event loop to exit immediately without blocking for events. I’ve also tried passing EV_TIMEOUT to event_set, this seems to have no effect at all.

Has anyone managed to get this working before? I know that the event_buffer_* stuff works with timeouts, however I want to use the standard event_base functions. One of the PECL bugs talks about event_timer_* functions and these functions do exist on my system, however they’re not documented at all.

  • 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-31T04:53:08+00:00Added an answer on May 31, 2026 at 4:53 am

    Problem is in fgets() in:

    printf("Received event: %s after %s\n%s", implode(getEventFlags($events)), getTimer(), fgets($fd));
    

    This blocks processing and waits for data from STDIN (but there are none on timeout)

    Change to something like that:

    $text = '';
    if ($events & EV_READ) {
        $text = fgets($fd);
    }
    printf("Received event: %s after %s\n%s", implode(getEventFlags($events)), getTimer(), $text);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... 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.