Share Variables/Memory Between All PHP Processes

Share variables/memory between all PHP processes

By default its simply not possible. Every solution will always copy the content into the current scope, because if not, there is no way to access it.

I dont know, what exactly want to do, but maybe you can do that "outside", for example as a gearman job, and then just catch the results of the process, instead of the whole array.

You can also think about splitting the "big" array into slices and then always retrieve the part you currently need from an apc or memcached.

Share variable between processes - PHP

i'm assuming you want to share the value of variable bar across all instances and sessions (just in-case), in that case the ideal thing will be to have the variable values stored in a dedicated file on the server and read by the variable when called. a similar trick is used in game development where DLL files are used as shared libraries..

i will further suggest having a script read the value of bar from the server probably by implementing a timer.

Sharing variables between child processes in PHP?

forked children will gain their own dedicated copy of their memory space as soon as they write anywhere to it - this is "copy-on-write". While shmop does provide access to a common memory location, the actual PHP variables and whatnot defined in the script are NOT shared between the children.

Doing $x = 7; in one child will not make the $x in the other children also become 7. Each child will have its own dedicated $x that is completely independent of everyone else's copy.

Does PHP copy variables when retrieving from shared memory?

This is the relevant C code snippet from sysvsem.c in PHP 5.2.9 :

/* setup string-variable and serialize */
/* get serialized variable from shared memory */
shm_varpos = php_check_shm_data((shm_list_ptr->ptr), key);

if (shm_varpos < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "variable key %ld doesn't exist", key);
RETURN_FALSE;
}
shm_var = (sysvshm_chunk*) ((char *)shm_list_ptr->ptr + shm_varpos);
shm_data = &shm_var->mem;

PHP_VAR_UNSERIALIZE_INIT(var_hash);
if (php_var_unserialize(&return_value, (const unsigned char **) &shm_data, shm_data + shm_var->length, &var_hash TSRMLS_CC) != 1) {
PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "variable data in shared memory is corrupted");
RETURN_FALSE;
}
PHP_VAR_UNSERIALIZE_DESTROY(var_hash);

PHP will have to unserialize the entire value every time you call shm_get, which, on a 50MB array, is going to be really really slow.

How about breaking it up into individual values?

Also you might want to consider using APC's variable cache, which will handle all of the shared memory and locking for you (and will also use a hash table for key lookups)

How to share memory between HTTP requests in PHP?

I wrote (forked from APC and maintain) APCu: A shared memory cache is not going to help you. Their internal storage area already has a defined structure, you can't change it. You can store your structure as objects, but these, and no other value, are actually shared between instances of PHP. Shared memory apc-like caches, copy out of shared memory for each context that requests the value.

I wrote pthreads (PHP extension): Threads are not going to help you. Just like APC having to copy out of shared memory, threads must.

PHP is shared nothing, all the time, or else you break stuff. You could write code that seemed as if it were sharing memory, but it wouldn't be; The rules must never be broken.

I don't think PHP a sensible target language if a primary requirement is efficiency, you seemed to recognize this by the end of your first paragraph. I can be wrong about that, but armed with all the facts above, I'd be surprised if you don't agree.

While it's not a sensible language, it's an arguably sensible platform. I'm going to assume that you want to use this in a web application context, and so are targeting PHP, but a much more sensible thing to do would be to implement the structures and algorithms in a suitable language, and expose it to your web application via an extension.

Suitable language generally means C or C++ for a PHP extension, but can mean others, if you are inventive enough.

You would still not be able to break the rules, but you wouldn't need to.

Obviously this relies on your ability to do those things.

Store PHP variables in memory between script executions

There is few options:

Memcache http://memcached.org/ extension. It's RAM based storage engine.

APC APC - PHP manual apc code cache allows store variables.

If you don't want any extensions you could store your data into file (serialize, or xml format), and it will be persistent data. Slower then memory storage.

And if you want to store general data, well then there is "one-hundred-two" database engines.
For example MySQL, SQLite or NOSQL MongoDB and more...



Related Topics



Leave a reply



Submit