Recently I've come face-to-face with a significant processing task for a web application written in PHP. I haven't worked with process control very much, so I started researching ways of distributing the calculations to multiple processes. PHP offers several libraries for doing this (pcntl, POSIX), but it quickly became clear that if you're running Windows these libraries are not an option, and unfortunately at work I have a Windows machine. After a lot more research, I came across Gearman.
Gearman is essentially a distributed processing framework, and seems to have community support for many programming languages. It consists of two main components: the job server, and a Client and Worker API. The Client and Worker API can be used in a wide variety of languages, but the job server is only available as a C library or a Perl library. This makes it a bit tougher to get the server running on Windows, especially when you start running into some of the dependencies that it requires to build. As well, the Client/Worker API for PHP can only be installed as a PECL extension, or a very-out-of-date PEAR extension called Net_Gearman.
Nonetheless, after yet more research I decided that I would give it a shot by using Cygwin to get the job server running (if you haven't used Cygwin before, be sure to read about it before attempting to install Gearman this way), and PEAR to use the API. Pre-built PECL extensions aren't available for Windows anymore, and the build process for PHP extensions can be pretty painful, so it makes PEAR look good by comparison even if the code will be out of date.
I had a pretty frustrating time finally getting everything up and running due to various dependency issues, so I went back through the whole process and wrote it out step-by-step. I used a Windows XP SP3 machine for this, but I also got it working on a Windows 7 machine as well.
If you're not already in the Cygwin setup, re-run the Cygwin setup.exe and go through to the package selection screen. The following is a list of dependency packages you will need in order to build the Gearman job server (gearmand). None of these packages were installed by default with Cygwin:
There's a good installation tutorial here that walks through getting gcc and make installed for people unfamiliar with Cygwin. Finding the others is pretty straightforward, the Search bar in the package selector works well.
Gearmand requires an event notification library called libevent that you cannot get as a Cygwin package, which means it has to be installed from source. You can get the source here.
cd to the unpacked libevent directory../configure
make
make install
libevent should now be installed and ready to be used when compiling the Gearman job server.
./configure
make
make install
The Gearman job server should now be installed and ready to use! Mine was installed at /usr/local/sbin/gearmand.exe, and running it with a "triple verbose" flag (-vvv) should produce the following:
That's it for the job server. When you want to start it, simply open a Cygwin shell and run gearmand.exe. Running it with the -d flag will cause the server to run as a daemon in the background, and running with --help will show you the full option list.
I chose to install the PEAR Client and Worker API, as it is native PHP and doesn't involve compiling PECL extensions. The PEAR package is called Net_Gearman, and was originally written by Joe Stump at Digg.com. It is old and out of date now, although there appears to be a more recent fork at http://github.com/brianlmoon/net_gearman. I stuck with the older version, as I suspect it will meet my needs, and was readily available as a PEAR package.
This also makes installation relatively painless. Assuming you've previously set PEAR up, then all you have to do is open a command window (not a Cygwin shell) and run:
pear install Net_Gearman-alpha
The "-alpha" portion is necessary, as Net_Gearman apparently never made it to a stable release version. That being said, it has functioned well for me so far. Perhaps someone will pick the project up in the future.
I'll write more about getting started with the Client and Worker API in the next article, so we can actually use Gearman to get some work done.
thanks amına koyam
Thank you for your tutorial.
Can you tell me how to run Net_Gearman?
Thanks,
David
Hi David,
I'm planning on doing a follow-up article at some point on how to utilize the basics of Net_Gearman once you've gotten the server up and running. Haven't had a chance to do it yet.
Great article but i am running into some problems with libevent. It looks like I might need to compile it as a shared library? I am using the verion you specified in your article. Any ideas? Thanks!
*** Warning: This system can not link to static lib archive /usr/local/lib/libev
ent.la.
*** I have the capability to make that library automatically link in when
*** you link to this library. But I can only do this if you have a
*** shared version of the library, which you do not appear to have.
CC libtest/server.lo
CCLD libtest/libserver.la
CC libtest/test.lo
CCLD libtest/libtest.la
CXX bin/arguments.o
CXX bin/function.o
CXX bin/gearman.o
CXX util/error.o
CXX util/pidfile.o
CXXLD bin/gearman.exe
util/error.o:/cygdrive/c/cygwinpackages/gearmand-0.17/util/error.cc:60: undefine
I take back my last comment. This might actually have to do with the latest version of cygwin. This is the actual error:
CXXLD bin/gearman.exe
util/error.o:/cygdrive/c/cygwinpackages/gearmand-0.17/util/error.cc:60: undefine
d reference to `____xpg_strerror_r'
collect2: ld returned 1 exit status
make[1]: *** [bin/gearman.exe] Error 1
make[1]: Leaving directory `/cygdrive/c/cygwinpackages/gearmand-0.17'
make: *** [all] Error 2
Wazzup Dear, What you ˙just written here really have me interested up to the last word, and I must tell you I rarely finish the full length post of blogs as I often got sick and tired of the junk that is presented in the junkyard of the world wide web on a daily basis and then I just end up checking out the headlines and maybe the first lines etc. But your headline and the first paragraphs were so cool and it immediately got me hooked. So, I just wanna say: nice and rare job! Thanks, really.
Thanks man. I love you. I have gearman running on bsd and being meaning to run it on my windows 7 machine for a LONG TIME. A couple of things I am using gearman.23. During the configure process it asked for boost headers and libuuid. I just search on cygwin and install anything related to those names. After that I was able to complete the gearman install. I am still not done yet. Have to work with the php client libraries now. On my BSD machine I have the gearman.so and not pear. I need both environments to be the same. Thank you for the detailed post
I cann't install gearmand-0.28.Maybe miss some cygwin packages.
$ make install
CXX libtest/libtest_libtest_la-binaries.lo
In file included from ./libtest/test.hpp:58:0,
from ./libtest/common.h:56,
from libtest/binaries.cc:23:
./libtest/cmdline.h:24:19: fatal error: spawn.h: No such file or directory
compilation terminated.
Makefile:4157: recipe for target `libtest/libtest_libtest_la-binaries.lo' failed
make: *** [libtest/libtest_libtest_la-binaries.lo] Error 1
Hey power,
I have the exact same problem. Any clue on how to solve it?