Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:79096 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 52321 invoked from network); 21 Nov 2014 21:33:25 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 21 Nov 2014 21:33:25 -0000 Authentication-Results: pb1.pair.com header.from=johannes@schlueters.de; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=johannes@schlueters.de; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain schlueters.de from 217.114.215.10 cause and error) X-PHP-List-Original-Sender: johannes@schlueters.de X-Host-Fingerprint: 217.114.215.10 mail.experimentalworks.net Received: from [217.114.215.10] ([217.114.215.10:50603] helo=mail.experimentalworks.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CD/10-50002-3AFAF645 for ; Fri, 21 Nov 2014 16:33:24 -0500 Received: by mail.experimentalworks.net (Postfix, from userid 1003) id A1B2847597; Fri, 21 Nov 2014 22:33:39 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on km31408.keymachine.de X-Spam-Level: X-Spam-Status: No, score=-1.0 required=3.0 tests=ALL_TRUSTED autolearn=unavailable version=3.3.2 X-Spam-HAM-Report: * -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP Received: from [192.168.2.34] (ppp-93-104-27-171.dynamic.mnet-online.de [93.104.27.171]) (using TLSv1.2 with cipher DHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: johannes@schlueters.de) by mail.experimentalworks.net (Postfix) with ESMTPSA id 8B09E47596; Fri, 21 Nov 2014 22:33:29 +0100 (CET) Message-ID: <1416605588.15061.64.camel@kuechenschabe> To: Tigran Bayburtsyan Cc: =?UTF-8?Q?=E5=85=B0_=E8=82=96=E9=B2=81?= , internals@lists.php.net Date: Fri, 21 Nov 2014 22:33:08 +0100 In-Reply-To: References: <1416479430.15061.15.camel@kuechenschabe> <1416483265.15061.26.camel@kuechenschabe> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.10.4-0ubuntu2 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] PHP SAPI module help From: johannes@schlueters.de (Johannes =?ISO-8859-1?Q?Schl=FCter?=) On Fri, 2014-11-21 at 23:06 +0400, Tigran Bayburtsyan wrote: > > And based on that now my main problem is that in PHP C++ sources there > are a lot of static, global variables which is not useful for me, to > clean that all for every request .... So I'm wondering to build > something which will help to somehow "save current state" of PHP > SAPI , and continue it on every request from saved source, without > sharing any variable between multiple requests. If you need to cleanup things between requests php_request_[startup| shutdown] are the functions you need which will do that. Basic flow in pseudocode with reference to pconn functions doing a bit more: /* once on application start, see also pconn_init_php() */ sapi_startup(); /* for each request, see pconn_do_request() */ while (!program_end()) { /* start request context */ php_request_startup(TSRMLS_C); /* we can multiple scripts in one request context */ while (get_script_for_current_request()) { if (source_is_in_a_file()) { php_execute_script(file_handle TSRMLS_CC); } else if (source_is_in_a_string()) { zend_eval_String(); } } /* clean up everything related to the request */ php_request_shutdown((void *) 0); } /* at the end of the application shutdown completely */ php_module_shutdown(TSRMLS_C); sapi_shutdown(); Now this uses global state. If you need multiple states parallel compile PHP in threaded mode (--enable-maintainer-zts manually at config time or force it by PHP_BUILD_THREAD_SAFE in your SAPI's custom config.m4. In that mode PHP globals which are request-specific (i.e. everything that happens in the while (!program_end()) loop) are bound to a windows thread or pthread (depending on platform). So if your event based system needs multiple PHP requests in parallel it has to create enough threads and route PHP work there. Handling multiple requests from a single thread in parallel is not supported from PHP's architecture (if you like adventures you might try to hack this by looking at tsrm_thread_id in TSRM/TSRM.c but that's not really a good way. Another approach is not embedding PHP directly in your process (which is dangerous anyways - PHP might crash due to stack overflow on some recursion patterns etc. which would kill your complete server) but by calling external PHP processes via FastCGI or FPM protocols, just like nginx or others do. johannes