Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:44960 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 6520 invoked from network); 14 Jul 2009 18:52:45 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Jul 2009 18:52:45 -0000 Authentication-Results: pb1.pair.com header.from=johannes@php.net; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=johannes@php.net; spf=unknown; sender-id=unknown Received-SPF: unknown (pb1.pair.com: domain php.net does not designate 83.243.58.134 as permitted sender) X-PHP-List-Original-Sender: johannes@php.net X-Host-Fingerprint: 83.243.58.134 mailout2.netbeat.de Linux 2.6 Received: from [83.243.58.134] ([83.243.58.134:45918] helo=mailout2.netbeat.de) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 4A/C8-54820-BF3DC5A4 for ; Tue, 14 Jul 2009 14:52:44 -0400 Received: (qmail 14778 invoked by uid 89); 14 Jul 2009 19:28:11 -0000 Received: from unknown (HELO ?192.168.1.102?) (johannes%schlueters.de@88.217.57.112) by mailout2.netbeat.de with ESMTPA; 14 Jul 2009 19:28:11 -0000 To: Thomas Koch Cc: gearman , internals@lists.php.net In-Reply-To: <200907141928.20591.thomas@koch.ro> References: <200907141928.20591.thomas@koch.ro> Content-Type: text/plain Date: Tue, 14 Jul 2009 20:52:39 +0200 Message-ID: <1247597559.31771.24.camel@goldfinger.johannes.nop> Mime-Version: 1.0 X-Mailer: Evolution 2.26.1 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Multithreaded C Gearman worker to run PHP scripts From: johannes@php.net (Johannes =?ISO-8859-1?Q?Schl=FCter?=) Hi, On Tue, 2009-07-14 at 19:28 +0200, Thomas Koch wrote: > So I'm happy for anybody interested to look over my work and help me to become > better: > http://github.com/thkoch2001/gearman-php-worker/tree/master You are wrapping every call inside PHP_EMBED_START_BLOCK/_END_BLOCK calls. By this you have a full PHP startup and shutdown on every single request to the worker. This costs quite some time and that startup/shutdown is not thread-safe which means multiple threads starting and finishing PHP will end with quite some trouble. For doing this you should write your own SAPI, for that take the sapi structure and create the helper functions (for a start take them from php_embed.c). In your main you then do the following (pseudo-code) void main() { /* param parsing and stuff ... */ tsrm_startup(1, 1, 0, NULL); sapi_startup(&gearman_module); /* now PHP is ready to handle requests */ startWorkers( host, port, scripts_list, numberOfWorkers ); /* after the last request finished and while shutting down: / php_module_shutdown(TSRMLS_C); sapi_shutdown(); tsrm_shutdown(); } and then in your runPHP it looks, using shortened and pseudo-code, like this: char *runPHP( char *scriptName ) { void ***tsrm_ls = NULL; tsrm_ls = ts_resource(0); /* actually you would do this when creating the thread and pass the TSRMLS macros along*/ if (php_request_startup(TSRMLS_C)==FAILURE) { return NULL; } /* prepare script handle and environment */ zend_try_first { php_execute_script(&scriptHandle TSRMLS_CC); } zend_catch { /* handle error */ } zend_end_try(); php_request_shutdown((void *) 0); } That's not tested and based on my mind and quick checking of my code, not the perfect final implementation but hopefully a good start. Please double check with the implementation of other SAPIs! For making development simple you might link such code against the embed sapi (and ignore php_embed.[ch]) at least the plumbing code should be moved into php-src/sapi/gearman or something and properly built without embed dependency. johannes