Page 1 of 1
convert string to unique integer
Posted: Sun Aug 08, 2010 3:25 pm
by shawngoldw
Yes, I know... it sounds strange... but I'm looking for a way to convert a string into an integer. I want to be able to take a string, convert it into an integer, and be able to convert it back into a string.
Why do I want to do this ask? (Or if you don't ask, skip the rest of this post

)
I'm working on a class which is meant to provide an interface to easily add parallel processing to a script. I posted the class in the coding critique forum but then later realised that something important was missing from it, the ability to pass information to and from the child process!
So in a search for how to do this, the answer that I've found is Semaphore,
http://ca.php.net/manual/en/ref.sem.php
With this set of functions I can set up a shared memory space for parent and child processes to both access and share information. But rather than identifying the different variables by a string semaphore requires integer identifiers.
What I would like to do is be able to say, in the child:
Code: Select all
$var = "myvar";
$value = "foo";
$id = str2id($var);
shm_put_var($shm, $id, $value);
Where $shm is a reference to the shared memory space.
Then later I want to be able to say, in the parent
Code: Select all
$var = "myvar";
$id = str2id($var);
$value = shm_get_var($shm, $id);
Otherwise I can just define some constants to use, but would need to set up constants for each possible variable I may need to use, which is really not ideal. For example:
Code: Select all
define("myvar", 1);
define("myothervar", 2);
etc.....
Thank you, and hope that's clear for the people who decided to read on

Re: convert string to unique integer
Posted: Sun Aug 08, 2010 3:40 pm
by JakeJ
I don't claim to know anything about shared memory spaces, etc. But why can't you just turn your string in to a variable and assign a random integer to it? Then you could pass that along in the shared memory space. Perhaps I'm missing something.
Re: convert string to unique integer
Posted: Sun Aug 08, 2010 4:14 pm
by shawngoldw
Interesting...
From you suggestion, I can create an associative array of variable name mapped to random ints in the parent. Then put the array of identifiers into the shared memory space on identifier 1. Then I can grab the random identifiers from the associative array in the shared space.
My mind is running in circles, I've never tried to deal with shared memory spaces and I've only dealt with parallel processing a TINY bit in java...
Thanks, I'll give this a try.
Re: convert string to unique integer
Posted: Sun Aug 08, 2010 5:22 pm
by JakeJ
This might make your brain explode but you can turn the contents of a variable in to a variable with $$variable. It
MIGHT make more sense to use variable variables but I'm not sure if that would be more efficient for you or not.
Here's an example:
Code: Select all
$x = "hello;
$$x = "world";
echo $hello;
//outputs "world"
even though you didn't declare $world as a variable, $$x makes the actual variable name $hello so you can refer to it directly. Of course if the contents of $x changes, you can no longer use $hello directly, you have to use echo ${$x};
Confused? Great! Check out the php manual page for a better explanation:
http://php.net/manual/en/language.varia ... riable.php
Re: convert string to unique integer
Posted: Sun Aug 08, 2010 6:42 pm
by shawngoldw
wow that is.. uhhh... ya...
I get it but It'll definitely take some time to wrap my head around that one.
Did I misunderstand your suggestion earlier? If I did could you please try to explain it further?
Re: convert string to unique integer
Posted: Sun Aug 08, 2010 6:57 pm
by JakeJ
No, I don't think you misunderstood me at all.
It's just that I know nothing about what you're trying to accomplish so I don't know if my suggestions are just stabs in the dark.
But it seems you're on the right track with your first response to me, I just wanted to give you that other option in case I was on the wrong track.
I sort of felt the same way about variable variables when I first looked at it too.
Re: convert string to unique integer
Posted: Sun Aug 08, 2010 9:14 pm
by shawngoldw
Well thanks for your help, I got it working like we talked about with the associative array, except instead of assigning random identifiers I just incremented them.
This is the class if you want to see:
Code: Select all
<?php
/*
* Run parallel process
*/
class Railway
{
// pids of running processes
var $pids;
// my pid
var $pid;
// shm references
var $shm;
// array of variable ids
var $var_id;
// pointers to return variables
var $returns;
// dispatch a new thread
function dispatch($file, &$params = null, &$return = null)
{
$this->threads++;
// start new thread
$pid = pcntl_fork();
if($pid == -1) // fork failed
{
return false;
}
$this->pid = getmypid();
$this->pids[] = $pid;
$this->_start_shm($pid);
if($return != null)
{
$i = 0;
$this->returns[$pid] =& $return;
foreach ($return as $key => $value)
{
$this->var_id[$pid][$value] = $i;
unset($return[$key]);
$i++;
}
}
if ($pid > 0) // parent
{
return true;
}
else // child
{
// include requested file
include($file);
exit;
}
}
// private
function _start_shm($pid)
{
if($pid == 0)
{
$pid = $this->pid;
}
$this->shm[$pid] = shm_attach($pid);
}
// only for use by children
function set_var($name, $value)
{
if(isset($this->var_id[0]))
{
$map = $this->var_id[0];
if(array_key_exists($name, $map))
{
$id = $map[$name];
shm_put_var(end($this->shm), $id, $value);
}
}
}
function _get_var($pid, $name)
{
$map = $this->var_id[$pid];
if(array_key_exists($name, $map))
{
$id = $map[$name];
$shm = $this->shm[$pid];
if(shm_has_var($shm, $id))
{
return shm_get_var($shm, $id);
}
else
{
return "";
}
}
else
{
return "";
}
}
// wait for all threads to finish
function wait(&$status = null)
{
//wait for each thread to finish
for ($i=0; $i < count($this->pids); $i++)
{
pcntl_wait($status);
}
foreach ($this->pids as $key => $value)
{
if(isset($this->var_id[$value]))
{
foreach ($this->var_id[$value] as $var => $id)
{
$return =& $this->returns[$value];
$return[$var] = $this->_get_var($value, $var);
}
}
$shm =& $this->shm[$value];
shm_remove($shm);
shm_detach($shm);
}
$this->pids = null;
$this->pid = null;
$this->shm = null;
$this->var_id = null;
$this->returns = null;
}
}
?>
I'll probably be going through it later some time just to clean it up, parts are confusing as it is now, and errors may pop up in unexpected places, it does work though so far.
With it you can split up a script into multiple parts and run them asynchronously to improve performance. You can pass variables to the scripts and through the shared memory space pass results back out.
I also benchmarked it for fun and found that the script is slower than running the processes normally one after another when a script is shorter than 35 - 50 milliseconds. If the script takes longer than 100 - 200 milliseconds to execute splitting it in half or thirds or even more and running it through this script is significantly faster.
I can see this being helpful when having to execute multiple complex sql queries which are unrelated to each other and can be queried and processed completely separately from each other.
Again, thanks for the tip!
Re: convert string to unique integer
Posted: Sun Aug 08, 2010 9:25 pm
by JakeJ
Incrementing is good too.
Just make sure you unset everything when you're done with it or you could end up running over the top of your own variables.
I'll keep that class in mind. I have an application that I'd like to be able to split some stuff up with, but I don't have time to deal with it just yet. I'll have to work on performance later.
Re: convert string to unique integer
Posted: Sun Aug 08, 2010 9:33 pm
by shawngoldw
Ya, I unset everything in the wait method, which is for waiting for all the processes to finish, I don't know if this is where I would like to be doing it or not, but it works for now.
If you try to use this later, feel free to ask me anything, as it is now its not really documented well at all.
Re: convert string to unique integer
Posted: Sun Aug 08, 2010 9:35 pm
by JakeJ
I'm always looking for good developers to bounce ideas off of in chat. Are you on GoogleTalk by any chance? If you are, send me a private message with your gmail address.
Re: convert string to unique integer
Posted: Sun Aug 08, 2010 9:37 pm
by shawngoldw
Naw, I don't use google chat.
Re: convert string to unique integer
Posted: Sun Aug 08, 2010 9:45 pm
by JakeJ
I used to use MSN Messenger a lot but I hardly use it at all anymore.
Re: convert string to unique integer
Posted: Mon Aug 09, 2010 9:57 am
by shawngoldw
I simplified the idea quite a bit. You don't need to define variable names, just create a variable, pass it in, and it will get populated. It works like return in any normal function except how it is called. Rather than writing return($var) in the child you write $this->set_return($var). This way you can return a simple string, array, obejct, or whatever into one value.
Code: Select all
/*
* Run parallel process
*/
class Railway
{
// pids of running processes
var $pids;
// shm references
var $shm;
// pointers to return variables
var $returns;
// dispatch a new thread
function dispatch($file, &$params = null, &$return = null)
{
$this->threads++;
// start new thread
$pid = pcntl_fork();
if($pid == -1) // fork failed
{
return false;
}
$this->returns[$pid] =& $return;
$this->_start_shm($pid);
if ($pid > 0) // parent
{
return true;
}
else // child
{
// include requested file
include($file);
exit;
}
}
// private
function _start_shm($pid)
{
$this->pids[] = $pid;
if($pid == 0)
{
$pid = getmypid();
}
$this->shm[$pid] = shm_attach($pid);
}
// only for use by children
function set_return($value)
{
shm_put_var(end($this->shm), 1, $value);
}
// wait for all threads to finish
function wait(&$status = null)
{
//wait for each thread to finish
for ($i=0; $i < count($this->pids); $i++)
{
pcntl_wait($status);
}
foreach ($this->pids as $key => $value)
{
$shm =& $this->shm[$value];
if(shm_has_var($shm, 1))
{
$this->returns[$value] = shm_get_var($shm, 1);
}
shm_remove($shm);
shm_detach($shm);
}
$this->pids = null;
$this->shm = null;
$this->returns = null;
}
}