Welcome Guest, Not a member yet? Register   Sign In
php globaly persistant variables for IPC status calls in ajax for synchronous processing - SHM? APC?
#1

[eluser]phazei[/eluser]
I have a question about how to go about uploading and then processing a file, all through ajax without ever reloading a page, but still getting status updates on what is going on.

I was going to have ajax calls for each part of the file process, that way when each part is done, the ajax gets a response and updates the user, then that triggers another ajax call.

But that seemed a bit complicated and a pain in the ass. The first call got the file location and name sent back, then the ajax sent that location and name to the next ajax call. Then I thought, maybe I can just store the file info in a session variable, and just send 'done' back in the ajax script and have it call the next function that way.

So I got to thinking, and want to just process the file all at once without needing to do another call at each step just to update the user.

I'm not quite certain how to go about it though. I was thinking of having some class variables which the process would update with the status, and then have another ajax function on an interval that retrieves those class variables. But I'm don't know enough to know how processes work in parallel. I've only ever just called one page at a time and have it do it's thing.

Would this work:
Code:
class doStuff() {
   public $status;
   public $count;

   function doing() {
      /*..stuff 1..*/
      $this->status = "stuff 1 done";
      /*..stuff 2..*/
      $this->status = "stuff 2 done";
      /*..stuff 3..*/
      $this->status = "stuff 3 done";
   }
   function status() {
      return $this->status;
   }
}
Then one ajax function will post to doStuff/doing one time, and the other would post to doStuff/status on an interval.


I don't know exactly how CI does instances so I don't know how the variables will persist at all. I was thinking maybe I could use sessions, but I (think I) know cookies don't update until the process is complete and since CI uses those instead, that wouldn't work. And even if it did use regular sessions, even though they update in a process right away, I don't know if they would update for all other processes right away as well.

I don't even know what I would need to google to learn more about it. I'm using PHP5. I saw something about 'static' but am not clear on that either, since I'm not declaring the class itself anywhere and am always working inside the classes in CI.


Please help a lost and confused (recently procedural only) programmer out.

Many thanks,
Adam
#2

[eluser]phazei[/eluser]
I found out I need IPC and can use SHM in PHP.


I'm still rather wobbly here though. Anyone have a shm library?
#3

[eluser]phazei[/eluser]
Just an fyi for anyone, the php shm_* and sem_* functions need to be compiled into PHP for it to work. Can't just add them like a module. Real pain. And I didn't notice that till I went to test the class I wrote, heh. Not very popular either since I guess PHP programmers don't have much of a need for IPC and synchronous code.

I'd highly suggest using APC (Alternative PHP Cache) instead. It can easily be added as a module, and works much simpler. I believe it has protection against race conditions.

It also has hooks to be able to show actual file upload progress which is really nice. It doesn't run on windows very well though. Hell, they stopped compiling the dll for download in windows and I can't find a new version anywhere. 3.0.0.19 is the last windows version though they are at 3.1.2 now. They even killed pecl4win.php.net Sad (Though I only use win for my own test server)
#4

[eluser]TheFuzzy0ne[/eluser]
If you have access to shell_exec(), you can shell_exec() a PHP file which can run in the background, updating a database or writing updates to a file. All consecutive Ajax calls could be fed the data from the file, and know what's happening.

It would take a bit more than that to get it to work, but I thought it might be a possibility for you. It would be down to you to implement it in a fashion that isn't resource intensive.
#5

[eluser]phazei[/eluser]
That's why I wanted to use shared memory, doing database calls or writing updates to the file is very resource intensive. I had found an IPC class suite that emulates shared memory with either files or dbs. It does have a class that uses shmop but I didn't like that since it doesn't store anything but strings.

APC lets me write to memory creating global persistent variables. I just need to make sure the key is unique so multiple users don't interfere with eachother.

Code:
$key = MYhash($username, $salt);
apc_store($key.'info',$data);
$filestatus = apc_fetch($key.'info');

So simple. $data can be any variable.

But I want to write it into a class so I can go:
Code:
$this->apc->put($key) = $data;
$this->apc->get($key) = $data;

But I don't know how to do that because both $key and $data need to be fed to the function as parameters. AFAIK I can't assign a function a variable...

Anyone have an idea?


I don't think I can overload the assignment operator in php... Sad




Theme © iAndrew 2016 - Forum software by © MyBB