Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:72704 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 82364 invoked from network); 20 Feb 2014 12:30:53 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Feb 2014 12:30:53 -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:55998] helo=mail.experimentalworks.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 9C/03-00813-C75F5035 for ; Thu, 20 Feb 2014 07:30:53 -0500 Received: from [192.168.2.31] (ppp-88-217-70-150.dynamic.mnet-online.de [88.217.70.150]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: johannes@schlueters.de) by mail.experimentalworks.net (Postfix) with ESMTPSA id 0EDD23FE9D; Thu, 20 Feb 2014 13:31:31 +0100 (CET) To: Tigran Bayburtsyan Cc: internals@lists.php.net In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Date: Thu, 20 Feb 2014 13:30:45 +0100 Message-ID: <1392899445.3990.785.camel@guybrush> Mime-Version: 1.0 X-Mailer: Evolution 2.30.3 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] PHP SAPI Embeded Backup problem From: johannes@schlueters.de (Johannes =?ISO-8859-1?Q?Schl=FCter?=) Hi, On Thu, 2014-02-20 at 12:44 +0400, Tigran Bayburtsyan wrote: > Hello. > I'm creating linux based C project and I need PHP Embeded interpreter , and > I've included it successfully , but now I need to run 2 parts of one PHP > script in 2 different threads. > Example: > part1: > > > part2: > > > part2 script will run many times , but part1 will run only one time. I > think I need something like backing up PHP interpreter state after part1 > script execution and restoring it before part2 script execution. > Is it possible with PHP SAPI ? Or what part of PHP source I need to edit > for making something like this. if everything should be in a single "request context" simply don't call request shutdown/startup before. The most simple way is #include "sapi/embed/php_embed.h" void runphp() { int argc = 0; char *argv[] = "myprogramm"; PHP_EMBED_START_BLOCK(argc, argv) zend_eval_string("$k = 15;", NULL, "embedded script 1" TSRMLS_CC); zend_eval_string("echo $k;", NULL, "embedded script 2" TSRMLS_CC); PHP_EMBED_END_BLOCK() } Some notes on that: * the PHP_EMBED_* macros wrap zend_[first_]try/zend_catch, thus if there is a fatal error in the first script the second one won't be executed * NULL in the eval calls is the return value which we ignore in this case (would be zval NULL in this case as there's no explicit return) * The "embedded script ?" strings are used in error messages ("Warning ... in embedded script 1 on line ..") * Use zend_execute_scripts() to execute scripts from file system or stream * In case you want to run PHP code from different C functions in the same PHP request context you have to * use php_embed_init() somewhere in your program before the first script * if PHP was built in thread-safe mode make sure to pass the TSRM context around (or use TSRMLS_FETH() ) and mind that PHP's thread context is bound to a system thread * if PHP wasn't build thread-safe make sure that only one thread at a time runs PHP APIs * wrap each zend_eval_string/zend_execute_scripts call in a zend_first_try / zend_catch block, else your program might terminate on a PHP runtime error * use php_embed_shutdown() after last PHP script finished * You can't easily mix different PHP states and switch between them, if you need multiple contexts each has to run on its own thread and PHP has to be built thread-safe, you then can't use php_embed_* but have to do the startups manually (global startup first, and request_startup once per thread) Hope that helps, johannes