Slig wrote:nocturne wrote: (have no idea how the threading is handled)...
I looked the source about it few days ago : it seems to be done by launching other php processes (no other way as php sadly still not have real multithread), and that sqlite is needed to transmit datas between processes (i think, as it is difficult to understand clearly with all clesses and subclasses involved). It seems to be some very complex and interesting part written by Flo, perhaps that he will read that thread and give us more details about it (essentially about how datas and signals are shared by processes)
The threading system indeed works by starting php childprocesses and let them communicate with the main process using sqlite. There is slightly differences for linux and windows behaviour.
On linux I have to check from the childprocesses if the main process is still running, this is done every second. Now when the main process is stopped all childs will exit. Without that check, you could have killed the main process but still have all threads running.
To be able to pass work to a thread you create a class that contains a method run(). This class needs to be instanciated, serialized and written into the database. Depending on the properties of this class it >can< be quite big, but normally it will be < 200 characters.
Child processes are checking the database regularly for new jobs and if there is one or more, they will take them in correct order, unserialize the objects and call the run method.
(there is some tricky stuff on which threads can get which jobs and that the jobs are fairly shared between the threadpool, but this is much detail. Also I won't be talking about the timings and the concrete realization, you can see the code for all details^^)
If the result is there it will once again be serialized and put back into the database.
The "ThreadPool" on the main process checks the results, removes them from the database and calls back with the command objects (that now have the result of the calculation).
The ThreadPool also keeps track of the thread's states. before a command is sent to a process, a ping is sent to check whether the process is still alive. another integrity check is that the ThreadPool knows when a command has been sent to a process and how long it is currently processing it.
If either a Thread is working too long or a ping is not being answered for a given time the thread will be "killed" (which means an intern exit signal is sent). A new thread will be started and the commands that have failed will be retransmitted. (If the killed thread will send some results after it has been killed they will be ignored). The threading system is not too fast, but one requirement was, that it should not loose any jobs
EDIT: Thanks Slig and nocturne for the details on how to launch a detached process on linux!