Page 1 of 2

Share Objects(resources) between multiple php threads

Posted: Sat Jul 28, 2007 7:50 pm
by john2496
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm desperately in need of a way to share objects between threads. I've used shared memory and other methods but I need to be able to pass around more than just value objects. Has anyone looked into writing an extension in php that will allow for the sharing of resource? For ex. I would like to be able to do the following...

Code: Select all

// thread1.php

class RandomInt
{
     private $int = 0;

     public function generate()
     {
         $this->int = rand(0, 100);
     }

    public function getInt()
    {
         return $int;
    }
}

$o = new RandomInt();

// our magical function that puts a pointer to the resource (or something) into fairy lang
set_shared_resource('random_int_object', $o);

while (true)
{
  $o->generate();
}

Code: Select all

// thread2.php

$e = get_shared_resource('random_int_object');

while (true)
{
    echo $e->getInt();
}
Anyway I can make this happen? I'm willing to take the time and write an extension if I know it can be done:/.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sun Jul 29, 2007 6:10 am
by Ollie Saunders
there are no threads in php. what do you think you need them for?

Posted: Mon Jul 30, 2007 4:18 am
by john2496
A cli server application of all things. There must be some way to share resources between processes that doens't involve simple data types.

Posted: Mon Jul 30, 2007 4:29 am
by miro_igov
Did you tried shared memory in combination of serializing the objects?

Posted: Mon Jul 30, 2007 4:56 am
by Ollie Saunders
There must be some way to share resources between processes that doens't involve simple data types.
A database.

Re: Share Objects(resources) between multiple php threads

Posted: Mon Jul 30, 2007 5:34 am
by stereofrog
john2496 wrote: I'm desperately in need of a way to share objects between threads. I've used shared memory and other methods but I need to be able to pass around more than just value objects. Has anyone looked into writing an extension in php that will allow for the sharing of resource?
I'm afraid you're out of luck with this one. Resource lists are maintained on a per-thread basis, so there's no way to share a resource between threads.

what about a web service

Posted: Mon Jul 30, 2007 6:11 am
by yacahuma
Do you need that much speed? Why not a web service?

Posted: Mon Jul 30, 2007 1:58 pm
by feyd
memcache and shmop may be of interest.

Posted: Mon Jul 30, 2007 2:06 pm
by john2496
Oh wellz :(. I'm really disappointed as I built a full blown game-server application that handles all of the game requests, path generation, entities management, interaction & collision detection, etc..., etc... Unfortunately the system is closed to the rest of the world except for a connection to the db & shared memory.

On the client side of things, it will dispatch a request to the server by inserting a record into the the database. It then writes the id of that record to a shared memory object that the server periodically polls.

Obviously there are a lot of issues with this solution including memory locking, db table locking issues, and other things :/. Any ideas? :/ Is it possible to write some kind of shared thread resources hack. I've been taking a stab at writing php extensions, however it seems that all of phps resources are managed by the zend enigne. How closed off the zend engine is, idk. I was hoping someone would have some advice or clarify :s.

Thanks,
John

Posted: Mon Jul 30, 2007 2:37 pm
by Begby
The best solution for this is to not use PHP. Use something that was designed from the start to use persistent objects on the server rather than trying to hack php. Its going to be more trouble than its worth.

Posted: Mon Jul 30, 2007 3:10 pm
by john2496
You're probably right, however, I will have this same break down in communication issue if I use any language other than php (assuming the client script is in php, and the server is in java, c, etc...). I'm considering a java approach but I am concerned a bit about speed and considering that java applications need to be logically divine, there will be tons and tons and tons of oop. I'm not opposed to oop, I love it, all of my code is full oop, its amazing, but I feel that if I use java I'll end up writing lots of excess code and stuff.

I am consdering the memcache approach and just finished installing it atm :). The only issue I'm concerned about with it, is locking and such :s.

Posted: Mon Jul 30, 2007 3:27 pm
by nathanr
I think you've hit a point I hit very very often.. I get so wrapped up in a project, I want to do it perfectly, and I can see the X is the perfect solution, but then mysql or php doesn't allow it. If I was you I'd take a step back, write down exactly how many times you'd use the functionailty in this project, check the size of the data you need to share and see if this approach is the most viable. It's worth remembering that mysql will cache results, thus a simple table for "global" variables could suffice, simply update at will, all clients run the select which hits the cache everytime, apart from directly after an update to the variable. It's also worth remembering that if you stick to myisam, the readlock will be shared over threads, and I'm pretty sure an update uses the same lock. it's only an insert tht requires an additional lock, which has to wait for all the select locks to clear before it can run, so it may well be a viable option.

Regardless though, you have touched on one of the many downsides to scripting languages such as PHP, and again a viable community project to solve this would certainly be worth persuing.

Posted: Mon Jul 30, 2007 4:36 pm
by john2496
Nathanr: Yes it seems that I've hit that point. But there is a solution :). The current system is REALLY inefficient and requires each client request to requery the entire entities table (literally thousands and thousands of entities) and then if the clients wants to tell the server to actually do something it has to insert a record into the 'requests' table and then write that insert id to a shared memory queue for processing by the server. There are obviously lots of problems with this approach, one of the major ones being that mysql is _much_ slower than shared memory. So the client has to enter a loop where it continously polls the 'responses' table awaiting a response from the server.

I've come to the realization that although shared resources in php might be possible with a super nifty extension, it is also really time consuming to write/debug/and design a system such as that.

memcached is a very attractive solution as it allows a thread-safe way to communicate between processes (something phps shared memory operations aren't good at, unless you design an entire seperate system to manage it).

Anyways, thats that for now :s. I'll keep you guys updated on any solutions that come my way.

Thanks :)!
John

Posted: Mon Jul 30, 2007 4:42 pm
by nathanr
This si probably the stupidest question ever.. but why doesn't the same script that inserts the request simply fire the script that does the mass update wait for a second then return the response?

Posted: Mon Jul 30, 2007 5:42 pm
by john2496
Not a stupid question, for many web apps that might work but in this case there can be 1000+ requests a minute easy. Not only would running the script during each request be inefficient but it would also create synchronization issues. :(.