Share Objects(resources) between multiple php threads

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

john2496
Forum Newbie
Posts: 7
Joined: Sat Jul 28, 2007 7:39 pm

Share Objects(resources) between multiple php threads

Post 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]
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

there are no threads in php. what do you think you need them for?
john2496
Forum Newbie
Posts: 7
Joined: Sat Jul 28, 2007 7:39 pm

Post 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.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Did you tried shared memory in combination of serializing the objects?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

There must be some way to share resources between processes that doens't involve simple data types.
A database.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Re: Share Objects(resources) between multiple php threads

Post 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.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

what about a web service

Post by yacahuma »

Do you need that much speed? Why not a web service?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

memcache and shmop may be of interest.
john2496
Forum Newbie
Posts: 7
Joined: Sat Jul 28, 2007 7:39 pm

Post 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
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post 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.
john2496
Forum Newbie
Posts: 7
Joined: Sat Jul 28, 2007 7:39 pm

Post 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.
User avatar
nathanr
Forum Contributor
Posts: 200
Joined: Wed Jun 07, 2006 5:46 pm

Post 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.
john2496
Forum Newbie
Posts: 7
Joined: Sat Jul 28, 2007 7:39 pm

Post 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
User avatar
nathanr
Forum Contributor
Posts: 200
Joined: Wed Jun 07, 2006 5:46 pm

Post 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?
john2496
Forum Newbie
Posts: 7
Joined: Sat Jul 28, 2007 7:39 pm

Post 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. :(.
Post Reply