How to exchange memory between HTTP requests in PHP?

I am trying to implement a very, very large dictionary to match the words in a sentence in PHP. My initial idea was to use the Aho-corasick algorithm, since Aho-corasick solves my exact problem. First I implemented Trie in PHP. Trie when caching makes a fairly fast dictionary; however, it takes up approximately 3 MB of memory. This will not scale well in PHP.

Obviously, regardless of the data structure that we use, a large dictionary will take up a lot of memory. I need only one instance of the dictionary, as it is static and will not need to be rebuilt.

If this object can be shared between all threads, 3 MB of memory is negligible, however I'm not sure about the correct way to share memory between threads in PHP.

How can I exchange this object between HTTP requests? I do not see the scaling of the project, when each thread requires 3 MB service information created only by Trie.

+5
source share
2 answers

I wrote (branched with APC and supports) APCu: the shared memory cache will not help you. Their internal storage area already has a certain structure, you cannot change it. You can store your structure as objects, but they and no other value are actually shared between PHP instances. Shared cached apc caches, copying from shared memory for each context that requests a value.

I wrote pthreads (PHP extension): threads will not help you. Just like APC, to copy from shared memory, threads must.

PHP doesn't share anything all the time, otherwise you are breaking material. You could write code that would seem to be memory sharing, but that would not be so; The rules must never be broken.

I donโ€™t think that PHP is a reasonable target language, if the main requirement is efficiency, you seem to have realized this by the end of your first paragraph. I may be mistaken in this, but armed with all the above facts, I would be surprised if you do not agree.

Although this is not an intelligent language, it is perhaps an intelligent platform. I assume that you want to use this in the context of a web application, and therefore target PHP, but a much more reasonable task would be to implement structures and algorithms in a suitable language and expose them to your web application through the extension.

A suitable language usually means C or C ++ to extend PHP, but may mean others if you are inventive enough.

You still canโ€™t break the rules, but you donโ€™t need to.

Obviously, it depends on your ability to do these things.

+5
source

You can do multithreading in php using PThreads.

https://github.com/krakjoe/pthreads

It uses posix streams and offers synchronization, thread pools, and read / write / execute support for stream objects.

It works on PHP7. Here is a two-counter program that runs asynchronously.

<?php $thread1 = new class extends Thread { public function run() { for ($i = 0; $i < 10000; $i++) { echo "Hello thread1 ($i)\n"; } } }; $thread2 = new class extends Thread { public function run() { for ($i = 0; $i < 10000; $i++) { echo "Hello thread2 ($i)\n"; } } }; $thread1->start() && $thread1->join(); $thread2->start() && $thread2->join(); ?> 
-3
source

Source: https://habr.com/ru/post/1233186/


All Articles