Re: PHP SAPI Embeded Backup problem

From: Date: Thu, 20 Feb 2014 12:30:45 +0000
Subject: Re: PHP SAPI Embeded Backup problem
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-72704@lists.php.net to get a copy of this message
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: > <?php $k = 15; ?> > > part2: > <?php echo $k; ?> > > 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

« previous php.internals (#72704) next »