I'm developing a game based on Html5, JavaScript (jQuery) and ajax with the use of PHP as Server Side. Now what is in the game is, there is a different types of jobs that you can assign to you players as per Job time duration. Suppose I have assign one jobJOB1
to one playerPLAYER1
with the duration of 10 minutes and other job JOB2
to other player PLAYER2
with the duration of 30 minuted. Means,
PLAYER1 = JOB1(10MINS)
PLAYER2 = JOB2(30MINS)
Now I want to create multithreading in PHP to handle this two jobs parallel and get the request to client side after every minute complete of that job.
I know that there are various ways to create multithreading in PHP and I can send the request to PHP server to create the thread when job is started with the use of ajax, but how can I get the data back to the client side and display the data after every minute?
It is possible or not? What are other options to do the same thing?
I think this is more of a task for multiprocessing rather than multithreading.
To create another PHP thread you would use something like pcntl_fork
:
http://php.net/manual/en/function.pcntl-fork.php
But in a request/response type situation, such as HTTP, you would more likely start a seperate process and then poll it from the client.
To do such a thing with PHP you would create another process on the CLI using the &
operator (linux only, if you need windows let me know and Ill update the answer), and he poll it using either:
There is a php-library called "spork" https://github.com/kriswallsmith/spork that is a wrapper arround the pcntl_fork functions.
it works pretty well and stable for me.