Rectangle 27 17

Use the Visual Studio Command Prompt, then

cd \where\you\put\it
nmake -f makefile.nmake

Or if you prefer the IDE, then File + New + Project, Visual C++, General node, pick the Makefile Project template. Name = libevent-2.0.10-stable, Location = parent directory (\where\you\put). OK. Next. Build command = nmake -f makefile.nmake, rest blank.

Just a note for others wanting to build libevent. Having been away from C/C++ for a long while, I made a nube assumtion that the libevent build they provided would output a libevent.dll. Instead it's examples which compile only required parts of libevent into their exe's.

But there is no nmake in Visual Studio 2008.

Sure there is, located in the vc\bin subdirectory. The Express edition is special.

c++ - How to build libevent version 2.0.10 with Visual Studio 2008 - S...

c++ c visual-studio-2008 build libevent
Rectangle 27 6

$ brew deps --installed
tmux: pkg-config libevent
q:
gdbm:
libxml2:
asciidoc: docbook
libevent:
pkg-config:
pcre:
docbook:
zsh: gdbm pcre
readline:
emacs: pkg-config

This seems to give us a list of all installed formulae including their dependencies. We can build a list of all formulae and a list of all dependencies and subtract the dependencies from the list of formulae, this should give us a list of formulae which are not dependencies of other formulae:

$ cat brew-root-formulae.sh
#!/bin/sh

brew deps --installed | \
    awk -F'[: ]+' \
    '{
        packages[$1]++
        for (i = 2; i <= NF; i++)
            dependencies[$i]++
    }
    END {
        for (package in packages)
            if (!(package in dependencies))
                print package
    }'
$ ./brew-root-formulae.sh
zsh
asciidoc
libxml2
readline
tmux
q
emacs

Is this the output you are after?

Thank you for your script, but I've found the answer from Denis to be better. 1) It's built-in. brew used to not have such a command, but now that it does it is better to use the built-in. 2) You script does show handle dupes well. For example if I have installed php55 which installs homebrew/dupes/zlib it will show zlib like it's not a dependency which is not true. Cheers!

Homebrew: List only installed top level formulas - Stack Overflow

homebrew
Rectangle 27 6

$ brew deps --installed
tmux: pkg-config libevent
q:
gdbm:
libxml2:
asciidoc: docbook
libevent:
pkg-config:
pcre:
docbook:
zsh: gdbm pcre
readline:
emacs: pkg-config

This seems to give us a list of all installed formulae including their dependencies. We can build a list of all formulae and a list of all dependencies and subtract the dependencies from the list of formulae, this should give us a list of formulae which are not dependencies of other formulae:

$ cat brew-root-formulae.sh
#!/bin/sh

brew deps --installed | \
    awk -F'[: ]+' \
    '{
        packages[$1]++
        for (i = 2; i <= NF; i++)
            dependencies[$i]++
    }
    END {
        for (package in packages)
            if (!(package in dependencies))
                print package
    }'
$ ./brew-root-formulae.sh
zsh
asciidoc
libxml2
readline
tmux
q
emacs

Is this the output you are after?

Thank you for your script, but I've found the answer from Denis to be better. 1) It's built-in. brew used to not have such a command, but now that it does it is better to use the built-in. 2) You script does show handle dupes well. For example if I have installed php55 which installs homebrew/dupes/zlib it will show zlib like it's not a dependency which is not true. Cheers!

Homebrew: List only installed top level formulas - Stack Overflow

homebrew
Rectangle 27 5

How can I pass the libevent option to setup.py using pip?

c:\pip install --help
(...)
--global-option=GLOBAL_OPTIONS
                      Extra global options to be supplied to the
                      setup.pycall before the install command

python - How to install gevent on Windows? - Stack Overflow

python pip gevent libevent
Rectangle 27 14

After a while, I realized that the paths for the CFLAGS variable mentioned above works when installing libevent from port, but not from brew. The following worked for me (on OSX Mavericks):

$ brew install libevent
$ export CFLAGS="-I /usr/local/Cellar/libevent/2.0.21/include -L /usr/local/Cellar/libevent/2.0.21/lib"
$ pip install gevent

can you please explain what does the second command do ?

It sets a few c-compiler flags needed for pip to be able to build and install gevent (en.wikipedia.org/wiki/CFLAGS)

macos - How can I install the Python library 'gevent' on Mac OS X Lion...

python macos osx-lion gevent
Rectangle 27 6

I was also running into this problem when installing apache Thrift and here is how I solved it for my system setup.

My operating system is El Capitan (10.11.1) with all the latest updates installed and I used MacPorts to install openssl (version 1.0.2e 3 Dec 2015). This installed openssl in /opt/local. In any case, try to find bio.h (for my case was located at /opt/local/include/openssl/bio.h) and you can probably tell where openssl is installed for your setup. When you are in the libevent folder and run:

./configure --help
--includedir=DIR        C header files [PREFIX/include]

Since PREFIX is /usr/local as per instructions, I just had to create a symlink in /usr/local/include.

cd /usr/local/include
sudo ln -s /opt/local/include/openssl/ openssl
make

thrift - fatal error: 'openssl/bio.h' file not found - Stack Overflow

openssl thrift libevent
Rectangle 27 2

I Confirm the goodness of syncless. It can use libev (the newer, cleaner and better performance version of libevent). Some times ago it doesn't has as much support as libevent has, but now the development process go further and is very useful.

networking - A clean, lightweight alternative to Python's twisted? - S...

python networking twisted asynchronous
Rectangle 27 3

I figured this out. As I was kind of suspected with the Memcached, memcache + libevent, and eAccelaretor from start, I just uninstalled these three services and now the webpage works fine.

Thanks for everyone help and support.

javascript - PHP stops page unexpectedly after executing some lines, s...

javascript php apache memcached rackspace-cloud
Rectangle 27 297

Your install is failing because you don't have the python development headers installed. You can do this through apt on ubuntu/debian with:

sudo apt-get install python3-dev

For eventlet you might also need the libevent libraries installed so if you get an error talking about that you can install libevent with:

sudo apt-get install libevent-dev
sudo yum install python-devel
sudo apt-get update
sudo apt-get install python-dev

I have the same error but have python-devel installed and system is updated with yum -y install updates.. what else could the issue be?

sudo apt-get install python2.6-dev

python - error: command 'gcc' failed with exit status 1 while installi...

python ubuntu eventlet
Rectangle 27 1

If you want to avoid threading / forking all together, I would recommend using all non-blocking I/O along with libevent. Libevent is fairly well known as a high performance solution for event driven programming.

c++ - best way to write a linux daemon - Stack Overflow

c++ linux daemon
Rectangle 27 2

Have you looked at PHPDaemon, which is written with active usage of libevent and pnctl? It has lots of features and even simple chat demo application. Even it has some production implementations.

That's very interesting, it seems like it'll get the job done. But can it be installed on a shared hosting? I wasn't clear from the documentation on where i need to upload the code to be able to run the installation command using SSH (I assume)

PHPDeamon is a backend server solution and therefor not a good solution for a shared hosting account.

javascript - How to implement a chat room using Jquery/PHP? - Stack Ov...

php javascript jquery ajax ajax-polling
Rectangle 27 27

I liked the concurrence Python module which relies on either Stackless Python microthreads or Greenlets for light-weight threading. All blocking network I/O is transparently made asynchronous through a single libevent loop, so it should be nearly as efficient as an real asynchronous server.

The downside is that its API is quite different from Python's sockets/threading modules; you need to rewrite a fair bit of your application (or write a compatibility shim layer)

Edit: It seems that there's also cogen, which is similar, but uses Python 2.5's enhanced generators for its coroutines, instead of Greenlets. This makes it more portable than concurrence and other alternatives. Network I/O is done directly with epoll/kqueue/iocp.

@intgr: great links. I had seen both of those before once upon-a-time, those are the sorts of things I was hoping to see flushed out. +1

Looks like concurrence is a dead project with their being the last update four years ago.

networking - A clean, lightweight alternative to Python's twisted? - S...

python networking twisted asynchronous
Rectangle 27 1

If you want to avoid threading / forking all together, I would recommend using all non-blocking I/O along with libevent. Libevent is fairly well known as a high performance solution for event driven programming.

c++ - best way to write a linux daemon - Stack Overflow

c++ linux daemon
Rectangle 27 6

I get "ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)" when I run: brew install libevent

macos - How can I install the Python library 'gevent' on Mac OS X Lion...

python macos osx-lion gevent
Rectangle 27 114

Don't post the entire thing! That's too much! 90% of the time, the first error is enough...

This means that the library which provides the event.h header is not installed. The library is called libevent (website).

In general, compilation errors like these are a flaw in the build scripts. The build script should give an error message that libevent is not installed, and it is a bug that it did not do so.

To get libevent from MacPorts and then manually tell compiler with CFLAGS environment variable where to find event.h and libevent while running pip.

sudo port install libevent
CFLAGS="-I /opt/local/include -L /opt/local/lib" pip install gevent

You can also use homebrew for installing libevent : brew install libevent (from David Wolever's comment)

Just to add: you can install libevent with homebrew using brew install libevent

By definition, the person asking the question is not qualified to judge which parts of the output are important, and hence they should always post the entire thing. Otherwise, important details sometimes get omitted.

@Mikael Lepist: Does that actually work? There's no -L flag, so I think you would get a link error.

CFLAGS="-I /opt/local/include -L /opt/local/lib"

@KristianGlass: Since you confirmed my expectations, I edited it in. I suspect Mikael Lepist may have some customized environment variables or something like that.

macos - How can I install the Python library 'gevent' on Mac OS X Lion...

python macos osx-lion gevent
Rectangle 27 4

I've solved this exact problem using what you mention, pipe() and libevent (which wraps epoll). The worker thread writes a byte to its pipe FD when its output queue goes from empty to non-empty. That wakes up the main IO thread, which can then grab the worker thread's output. This works great is actually very simple to code.

Thank you for being the first person to understand my problem! I was hoping to avoid the pipe. It seems kind of kludgey, but if that's how it has to be done, so be it.

@Omnifarious - I understood your problem. I may not have explained myself clearly. With MQs you don't need the extra pipe. You put the mqd_t of your worker output queue in your select set. When one of the workers puts something in the output queue your main loop will be notified.

Just make sure you make your file descriptor non-blocking. The MQ stuff looks neat -- I'd be interested to know if there are any problems hooking it into libevent.

I never used libevent but if it just wraps epoll then it should work. Man (7) mq_overview specifically says "On Linux, a message queue descriptor is actually a file descriptor, and can be monitored using select(2), poll(2), or epoll(7). This is not portable."

I was tempted to pick yours because this is what I'm likely actually going to do. It has the advantage of only requiring system calls in certain specific situations (sometime after non-empty queue becomes empty). And those situations are less (not more) likely to occur during times of high load. But I picked the most unusual and interesting alternate method instead.

c++ - I want to wait on both a file descriptor and a mutex, what's the...

c++ python linux multithreading event-driven
Rectangle 27 20

I'm on El Capitan 10.11.6 and i had the issue while installing manually the mongodb php extension.

I solved it following the indications of @user7059092 about the configure stage :

$./configure LDFLAGS='-L/usr/local/opt/openssl/lib' CPPFLAGS='-I/usr/local/opt/openssl/include'

thanks alot. I have been stuck on the issue for a while. You solution worked for me

I can confirm that this works while installing thrift as well. Configuration should be done before the make.

thrift - fatal error: 'openssl/bio.h' file not found - Stack Overflow

openssl thrift libevent
Rectangle 27 45

Install beanstalkd. Another message queue, BUT this one supports DELAYED PUTS. If you compile from source it is going to be a little harder because it depends on libevent(like memcached). But then again, I don't think you have to compile it from source, because there are a lot of binary packages available. For example on Ubuntu you can install beanstalkd by issuing the command:

sudo apt-get install beanstalkd

Install a beanstalkd client library. The best one I found was node-beanstalk-client. Because on the beanstalkd client library list this library isn't/wasn't mentioned(Then again I can add entries to the list, so I will add this one). The reasons I prefer this library over the others are:

  • Npm package: I liked to use a npm package to install client library. The others did not have any.
  • Active development: I prefer libraries which have later/more commits.

So to install it, after you have installed npm(the write way) you would just issue the following command:

npm install beanstalk_client
node consumer.js

Next start producer.js. Five seconds(delay I specified) after you execute producer.js, consumer.js will process the message.

var client = require('beanstalk_client').Client;
client.connect('127.0.0.1:11300', function(err, conn) {
    var job_data = {"data": {"name": "node-beanstalk-client"}};
    var priority = 0;
    var delay = 5;
    var timeToRun = 1;
    conn.put(priority, delay, timeToRun, JSON.stringify(job_data), function(err, job_id) {
        console.log('put job: ' + job_id);
        process.exit();
    });
});
node producer.js

Great answer. Sad OP hasn't marked as correct.

delayed job - Node.js workers/background processes - Stack Overflow

node.js delayed-job resque
Rectangle 27 2

Concerning "the Rest" of the Code

Event extension is very appropriate. It is a port of Libevent library which is designed for event-driven I/O, mainly for networking.

I have written a sample HTTP client that allows to schedule a number of HTTP requests and run them asynchronously.

The class allows to schedule a number of HTTP requests, then run them asynchronously.

<?php
class MyHttpClient {
  /// @var EventBase
  protected $base;
  /// @var array Instances of EventHttpConnection
  protected $connections = [];

  public function __construct() {
    $this->base = new EventBase();
  }

  /**
   * Dispatches all pending requests (events)
   *
   * @return void
   */
  public function run() {
    $this->base->dispatch();
  }

  public function __destruct() {
    // Destroy connection objects explicitly, don't wait for GC.
    // Otherwise, EventBase may be free'd earlier.
    $this->connections = null;
  }

  /**
   * @brief Adds a pending HTTP request
   *
   * @param string $address Hostname, or IP
   * @param int $port Port number
   * @param array $headers Extra HTTP headers
   * @param int $cmd A EventHttpRequest::CMD_* constant
   * @param string $resource HTTP request resource, e.g. '/page?a=b&c=d'
   *
   * @return EventHttpRequest|false
   */
  public function addRequest($address, $port, array $headers,
    $cmd = EventHttpRequest::CMD_GET, $resource = '/')
  {
    $conn = new EventHttpConnection($this->base, null, $address, $port);
    $conn->setTimeout(5);

    $req = new EventHttpRequest([$this, '_requestHandler'], $this->base);

    foreach ($headers as $k => $v) {
      $req->addHeader($k, $v, EventHttpRequest::OUTPUT_HEADER);
    }
    $req->addHeader('Host', $address, EventHttpRequest::OUTPUT_HEADER);
    $req->addHeader('Connection', 'close', EventHttpRequest::OUTPUT_HEADER);
    if ($conn->makeRequest($req, $cmd, $resource)) {
      $this->connections []= $conn;
      return $req;
    }

    return false;
  }


  /**
   * @brief Handles an HTTP request
   *
   * @param EventHttpRequest $req
   * @param mixed $unused
   *
   * @return void
   */
  public function _requestHandler($req, $unused) {
    if (is_null($req)) {
      echo "Timed out\n";
    } else {
      $response_code = $req->getResponseCode();

      if ($response_code == 0) {
        echo "Connection refused\n";
      } elseif ($response_code != 200) {
        echo "Unexpected response: $response_code\n";
      } else {
        echo "Success: $response_code\n";
        $buf = $req->getInputBuffer();
        echo "Body:\n";
        while ($s = $buf->readLine(EventBuffer::EOL_ANY)) {
          echo $s, PHP_EOL;
        }
      }
    }
  }
}


$address = "my-host.local";
$port = 80;
$headers = [ 'User-Agent' => 'My-User-Agent/1.0', ];

$client = new MyHttpClient();

// Add pending requests
for ($i = 0; $i < 10; $i++) {
  $client->addRequest($address, $port, $headers,
    EventHttpRequest::CMD_GET, '/test.php?a=' . $i);
}

// Dispatch pending requests
$client->run();

This is a sample script on the server side.

php http-client.php
Success: 200
Body:
GET: array (
  'a' => '1',
)
User-Agent: My-User-Agent/1.0
Success: 200
Body:
GET: array (
  'a' => '0',
)
User-Agent: My-User-Agent/1.0
Success: 200
Body:
GET: array (
  'a' => '3',
)
...

Note, the code is designed for long-term processing in the CLI SAPI.

For custom protocols, consider using low-level API, i.e. buffer events, buffers. For SSL/TLS communications, I would recommend the low-level API in conjunction with Event's ssl context. Examples:

Although Libevent's HTTP API is simple, it is not as flexible as buffer events. For example, the HTTP API currently doesn't support custom HTTP methods. But it is possible to implement virtually any protocol using the low-level API.

I have also written a sample of another HTTP client using Ev extension with sockets in non-blocking mode. The code is slightly more verbose than the sample based on Event, because Ev is a general purpose event loop. It doesn't provide network-specific functions, but its EvIo watcher is capable of listening to a file descriptor encapsulated into the socket resource, in particular.

Ev extension implements a simple yet powerful general purpose event loop. It doesn't provide network-specific watchers, but its I/O watcher can be used for asynchronous processing of sockets.

The following code shows how HTTP requests can be scheduled for parallel processing.

<?php
class MyHttpRequest {
  /// @var MyHttpClient
  private $http_client;
  /// @var string
  private $address;
  /// @var string HTTP resource such as /page?get=param
  private $resource;
  /// @var string HTTP method such as GET, POST etc.
  private $method;
  /// @var int
  private $service_port;
  /// @var resource Socket
  private $socket;
  /// @var double Connection timeout in seconds.
  private $timeout = 10.;
  /// @var int Chunk size in bytes for socket_recv()
  private $chunk_size = 20;
  /// @var EvTimer
  private $timeout_watcher;
  /// @var EvIo
  private $write_watcher;
  /// @var EvIo
  private $read_watcher;
  /// @var EvTimer
  private $conn_watcher;
  /// @var string buffer for incoming data
  private $buffer;
  /// @var array errors reported by sockets extension in non-blocking mode.
  private static $e_nonblocking = [
    11, // EAGAIN or EWOULDBLOCK
    115, // EINPROGRESS
  ];

  /**
   * @param MyHttpClient $client
   * @param string $host Hostname, e.g. google.co.uk
   * @param string $resource HTTP resource, e.g. /page?a=b&c=d
   * @param string $method HTTP method: GET, HEAD, POST, PUT etc.
   * @throws RuntimeException
   */
  public function __construct(MyHttpClient $client, $host, $resource, $method) {
    $this->http_client = $client;
    $this->host        = $host;
    $this->resource    = $resource;
    $this->method      = $method;

    // Get the port for the WWW service
    $this->service_port = getservbyname('www', 'tcp');

    // Get the IP address for the target host
    $this->address = gethostbyname($this->host);

    // Create a TCP/IP socket
    $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if (!$this->socket) {
      throw new RuntimeException("socket_create() failed: reason: " .
        socket_strerror(socket_last_error()));
    }

    // Set O_NONBLOCK flag
    socket_set_nonblock($this->socket);

    $this->conn_watcher = $this->http_client->getLoop()
      ->timer(0, 0., [$this, 'connect']);
  }

  public function __destruct() {
    $this->close();
  }

  private function freeWatcher(&$w) {
    if ($w) {
      $w->stop();
      $w = null;
    }
  }

  /**
   * Deallocates all resources of the request
   */
  private function close() {
    if ($this->socket) {
      socket_close($this->socket);
      $this->socket = null;
    }

    $this->freeWatcher($this->timeout_watcher);
    $this->freeWatcher($this->read_watcher);
    $this->freeWatcher($this->write_watcher);
    $this->freeWatcher($this->conn_watcher);
  }

  /**
   * Initializes a connection on socket
   * @return bool
   */
  public function connect() {
    $loop = $this->http_client->getLoop();

    $this->timeout_watcher = $loop->timer($this->timeout, 0., [$this, '_onTimeout']);
    $this->write_watcher = $loop->io($this->socket, Ev::WRITE, [$this, '_onWritable']);

    return socket_connect($this->socket, $this->address, $this->service_port);
  }

  /**
   * Callback for timeout (EvTimer) watcher
   */
  public function _onTimeout(EvTimer $w) {
    $w->stop();
    $this->close();
  }

  /**
   * Callback which is called when the socket becomes wriable
   */
  public function _onWritable(EvIo $w) {
    $this->timeout_watcher->stop();
    $w->stop();

    $in = implode("\r\n", [
      "{$this->method} {$this->resource} HTTP/1.1",
      "Host: {$this->host}",
      'Connection: Close',
    ]) . "\r\n\r\n";

    if (!socket_write($this->socket, $in, strlen($in))) {
      trigger_error("Failed writing $in to socket", E_USER_ERROR);
      return;
    }

    $loop = $this->http_client->getLoop();
    $this->read_watcher = $loop->io($this->socket,
      Ev::READ, [$this, '_onReadable']);

    // Continue running the loop
    $loop->run();
  }

  /**
   * Callback which is called when the socket becomes readable
   */
  public function _onReadable(EvIo $w) {
    // recv() 20 bytes in non-blocking mode
    $ret = socket_recv($this->socket, $out, 20, MSG_DONTWAIT);

    if ($ret) {
      // Still have data to read. Append the read chunk to the buffer.
      $this->buffer .= $out;
    } elseif ($ret === 0) {
      // All is read
      printf("\n<<<<\n%s\n>>>>", rtrim($this->buffer));
      fflush(STDOUT);
      $w->stop();
      $this->close();
      return;
    }

    // Caught EINPROGRESS, EAGAIN, or EWOULDBLOCK
    if (in_array(socket_last_error(), static::$e_nonblocking)) {
      return;
    }

    $w->stop();
    $this->close();
  }
}

/////////////////////////////////////
class MyHttpClient {
  /// @var array Instances of MyHttpRequest
  private $requests = [];
  /// @var EvLoop
  private $loop;

  public function __construct() {
    // Each HTTP client runs its own event loop
    $this->loop = new EvLoop();
  }

  public function __destruct() {
    $this->loop->stop();
  }

  /**
   * @return EvLoop
   */
  public function getLoop() {
    return $this->loop;
  }

  /**
   * Adds a pending request
   */
  public function addRequest(MyHttpRequest $r) {
    $this->requests []= $r;
  }

  /**
   * Dispatches all pending requests
   */
  public function run() {
    $this->loop->run();
  }
}


/////////////////////////////////////
// Usage
$client = new MyHttpClient();
foreach (range(1, 10) as $i) {
  $client->addRequest(new MyHttpRequest($client, 'my-host.local', '/test.php?a=' . $i, 'GET'));
}
$client->run();
http://my-host.local/test.php
$_GET
<?php
echo 'GET: ', var_export($_GET, true), PHP_EOL;

Then the output of php http-client.php command will be similar to the following:

<<<<
HTTP/1.1 200 OK
Server: nginx/1.10.1
Date: Fri, 02 Dec 2016 12:39:54 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: close
X-Powered-By: PHP/7.0.13-pl0-gentoo

1d
GET: array (
  'a' => '3',
)

0
>>>>
<<<<
HTTP/1.1 200 OK
Server: nginx/1.10.1
Date: Fri, 02 Dec 2016 12:39:54 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: close
X-Powered-By: PHP/7.0.13-pl0-gentoo

1d
GET: array (
  'a' => '2',
)

0
>>>>
...

Note, in PHP 5 the sockets extension may log warnings for EINPROGRESS, EAGAIN, and EWOULDBLOCK errno values. It is possible to turn off the logs with

error_reporting(E_ERROR);

I just want to do something like file_get_contents(), but not wait for the request to finish before executing the rest of my code.

The code that is supposed to run in parallel with the network requests can be executed within a the callback of an Event timer, or Ev's idle watcher, for instance. You can easily figure it out by watching the samples mentioned above. Otherwise, I'll add another example :)

http - Asynchronous PHP calls? - Stack Overflow

php http asynchronous
Rectangle 27 2

Also try Syncless. It's coroutine-based (so it's similar to Concurrence, Eventlet and gevent). It implements drop-in non-blocking replacements for socket.socket, socket.gethostbyname (etc.), ssl.SSLSocket, time.sleep and select.select. It's fast. It needs Stackless Python and libevent. It contains a mandatory Python extension written in C (Pyrex/Cython).

networking - A clean, lightweight alternative to Python's twisted? - S...

python networking twisted asynchronous