Jump to content

enable threads in php


jessPHP

Recommended Posts

 

Hi everyone!!

 

I am developing a concurrent websocket server in PHP. I prefer to implement it using pthreads rather than using fork to create new processes, because the server is supposed to handle thousands of connections, and threads are lighter than processes.

 

But when I execute this code:

 

 

<?php
class My extends Thread {
public function run() {
/** ... **/
}
}
$my = new My();
var_dump($my->start());
?>

 

I get this error:

 

 

PHP Fatal error: Class 'Thread' not found in /home/user/htdocs/example/server.php on line 3

 

Why?? How can I get pthreads working in PHP??

 

By the way, do you have another suggestions to implement the concurrent server??

 

I am using Ubuntu 12.04 64bits

 

Thank you very much!

Link to comment
Share on other sites

You'd need to build your own copy of PHP. Threads is not something that is standard with PHP, it is a PECL extension that you'd have to download and build. Look at the user notes on the installation page for some details.

 

Depending on what exactly this server is supposed to do, you may not even need threads or processes to handle concurrent connections. Using non-blocking IO on the sockets will allow you to handle reading/writing multiple connections. You'd just have to make sure that processing for the sockets does not cause the script to block.

Link to comment
Share on other sites

 

Hi kicken, thank you very much for your response.

 

I am not sure, but I think I need concurrent server because each client connection needs an unique communication.

 

 

 

This would be my code:

 

$client_sock = socket_accept($socket);

$pid_child = pcntl_fork();

 

if($pid_child ==0)

{

while(true)

{

$input = socket_read($client_sock, 100);

if($input) handle_message($input);

else

break;

}

 

socket_close($client_sock);

//send a message to the rest of clients, telling them that this client went offline

}

 

[code]

 

 

I am developing an online game, where several users can invite other users to play. Besides, each user can send a private message to another user, something like a chat.

 

Another thing I need, is to implement a shared list of users, using some IPC technique, but I do not know what to use...

 

Thank you again!

Link to comment
Share on other sites

I'll assume you got as far as executing ...

 

pthreads objects can be shared and manipulated among contexts, the problem of sharing a list of users between threads safely is a non-problem:

 

<?php/* Note: this example is not well synchronized, sometimes numbers and data may mismatch*/class UserList extends Stackable {    public function run(){}} class UserThread extends Thread {    public function __construct(UserList $shared) {        $this->shared = $shared;    }    public function run() {        while (($num = count($this->shared))) {                        printf("%lu finds %s users\n", $this->getThreadId(), $num);            var_dump($this->shared);                        /* randomly remove a user so this thread has an end */            if (time() % 2 == 0)                $this->shared->shift();                        /* sleep is bad, but you want to read output */            sleep(1);        }    }} /* create a list of users */$list = new UserList(); /* fill a list of users */$list->merge(array(    "harry", "barry", "sue", "sally")); /* create some threads */$threads = array();$thread = 0; while ($thread < count($list)) {    $threads[$thread] = new UserThread($list);    $threads[$thread]->start();    $thread++;} /* you can still manipulate the list from here */$list[] = "larry";$list[] = "carry";$list[] = "krakjoe"; /* join some threads */foreach ($threads as $thread)    $thread->join();?>

 
In the example above you can see how an object can be manipulated by all contexts at the same time, hope that makes things clearer ...
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.