PHP can't open file in thread running context - php

i' m working on a monitor for a structure composed by a lot of computers. I develop it in PHP and i want to ping all the structure by the quickest way.
For this i use the multi-thread inherited from pthread enter link description here So i follow some tutorials and finally i have one class (extended from Thread) and a caller in an other script :
The class :
class Ping extends Thread{
public $id;
public $name;
public function __construct($id, $name){
$this->id = $id;
$this->name = $name;
}
public function run(){
$ping = exec("ping -n 1 -w 80 " . $this->name);
$h = fopen("ping.json", 'w');
if(preg_match("#perte 100#", $ping)){
fwrite($h,'d');
}
else {
fwrite($h,'c');
}
fclose($h);
}
}
The caller :
$p = array();
foreach($array_computer as $comp){
array_push($p, new Ping(array_search ($comp , $array_computer), $comp->{'name'}));
}
foreach ($p as $p_t){
$p_t->start(PTHREADS_INHERIT_ALL);
}
So i have two problems :
1 . When i want to echo $id or $name, nothing is display
2 . I can't open 'ping.json' because : "failed to open stream: Permission denied"
If i replace
$p_t->start(PTHREADS_INHERIT_ALL);
by
$p_t->run();
The call works but i lose the interest of multithread :P

Could it be var $p_t that is an instance of Ping?
foreach ($p as $p_t)
Also try checking for instance ie.
if ($p_t instanceof Ping) {
$p_t->start();
}

Some question/remarks to help you along:
you are writing something to a file based on a computername, but has
this name a value? Otherwise the output file will be empty and you
won't get a result.
Within the foreach, echo the parameters you want to store in $p so
you know you are putting something in there.
where does the start method come from? Is $p an array of objects
object? Is so, where is the method declared?
$comp->{'name'} looks odd to me; why not use $comp->name if it's an
object or $comp['name'] if it's an array?
you are not storing any resulting values in your public variables; therefore
when an input value is blank, it wil stay blank.

Thanks for answers,
i found an other solution by storing data results in a database into the run() function.
I think that some conflicts appeared when i want to write the json file at the same time, and lock the file with flock() wasn't efficient.
I put the Ping class and the caller in the same file and ordinate the thread excution like this :
$i = 0;
foreach ($p as $p_t){
while($i > 15){}
$i++;
$p_t->start();
if($p_t->join())$i--;
}

Related

Convert a string to function (callable) and keep it cached

I'm trying to make a little benchmarking script where I can enter short pieces of code for quick evaluation of my anticipations. I imagine it similar to jsPerf (but password-protected for security reasons).
The main loop should look like this:
public function run(&$t, $count) {
//Run setup function
if(is_callable($this->setup))
call_user_func($this->setup);
//Save inital time
$t($this->name);
//THE MAIN LOOP
for($i=0; $i<$count; $i++) {
call_user_func($this->fn);
}
//Save end time
$t($this->name."_end");
//return time difference
return $t[$this->name."-".$this->name."_end"];
}
However, this will only work with static approach - with functions defined while making the script:
//New instance of tester
$b = new Benchmarker();
$b->add(
//Name
"touch",
//closure
function() {
touch("file.txt");
},
//Code seen in final reports
"touch()"
);
So as you see, I use call_user_func, not eval. Besides the fact that it's evil function in it's nature, I want to avoid it for performance reasons. If I'm testing a code that takes about 10ns to process and eviluation takes about 100ns, my results will be rather random.
This is why I'm looking for a way to convert string to a callable object. You can think about it like one-time eval.
$callable = string_to_callable("function() {echo \"Hello world!\";}");
$b->add(
//Name
"echo",
//callable object
$callable,
//Code seen in final reports
"echo \"...\""
);
Is that possible?
Note:
I can see funny workaround using include:
//Code received from the user
$code = "echo \"Hello world!\";";
//Random name for a new function
$rndname = "fn_".rand(0,100000); //There are smarter ways to do this of course
//String of the new function
$func = "function $rndname() {{$code}}";
//Define a filename
$f = $rndname.".php";
//Put the code in the file
file_put_contents($f, "<?php\n$func\n?".">");
//Include the new script
include $f;
//Call the function
call_user_func($rndname);
//Delete the file
unlink($f);
I really do hope that I won't need the code above!
Apart from creating a new file, there may be a closure trick:
function string_to_callable($string) {
return eval("return function() {{$string}};");
}

How to store values globally, permantly?

I want to store some counter and want to increment as desired.
This counters are not related to any client, so i cant use session or cookies.
I tried $GLOBALS but, its not what i want.
I want something like, let say i have 3 php files, each will do some counter manuplation.
init.php
$_GLOBAL_VARIABLE['cntr1'] = 0;
file1.php
$_GLOBAL_VARIABLE['cntr1'] = $_GLOBAL_VARIABLE['cntr1'] + 7;
file2.php
$_GLOBAL_VARIABLE['cntr1'] = $_GLOBAL_VARIABLE['cntr1'] + ($_GLOBAL_VARIABLE['cntr1'] * 0.90);
file3.php
echo $_GLOBAL_VARIABLE['cntr1'];
All three files (except init.php) will called randomly without ant relation and init will called once.
I dont want to try database transaction coz counter manuplation is very frequent, and file i/o is one and the same. I am looking for some way to store my data on server till the time its up and running, somewhat like global class and variables in c#.
If you want the store globally accessible value in the server without the use of database, cookie or session then memcache could be a solution for you. Its a daemon which allows you to store data and use it across different connection requests. If you have frequent visits you will have to somehow handle concurrency within you application.
I think this will work
global $cntr1;
If not then you can make one .inc.php and include this file to all pages.
This will resolve your issue.
im sure this is NOT what you wanted, but i've used fileread/filewrite to store my globals in a file on the drive, which can be read from, written to with updated values etc. This allows for you to set MANY global variables as int's, I've modified my globals code to work as a iterator, counting up or counting down by what ever value you pass.
its a simple quick class i made to handle the request :
<?php
class my_global{
protected $name;
protected $value;
static protected $path = './globals/';
public function __construct()
{
if(!is_dir(self::$path))
mkdir(self::$path);
}
public function change($name, $value)
{
$current = $this->get($name);
$this->set($name,$current+$value);
return $current+$value;
}
protected function set($name, $value)
{
$this->name = $name;
$this->value = $value;
$this->write();
}
protected function get($name)
{
if(file_exists(self::$path.$name))
{
$myFile = self::$path.$name;
$fh = fopen($myFile, 'r');
$value = fread($fh, filesize($myFile));
fclose($fh);
}
else
$value = 0;
$this->name = $name;
$this->value = $value;
return $value;
}
protected function write(){
$myFile = self::$path.$this->name;
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $this->value);
fclose($fh);
}
}
$my_global = new my_global();
?>
You can then just call the $my_global->change() method to increase or decrease the counter
<?php
echo $my_global->change('new_global',5).'<br>';
echo $my_global->change('anotherglobal',-2).'<br>';
echo $my_global->change('forme',7).'<br>';
?>
this is more food for thought than anything, but could be used tweaked to work as you need it.

How to touch a file and read the modification date in PHP on Linux?

I need to touch a file from within one PHP script and read the last time this file was touched from within another script, but no matter how I touch the file and read out the modification date, the modification date doesn't change, below is a test file.
How can I touch the log file and thus change the modification date, and then read this modification date?
class TestKeepAlive {
protected $log_file_name;
public function process() {
$this->log_file_name = 'test_keepalive_log.txt';
$this->_writeProcessIdToLogFile();
for ($index = 0; $index < 10; $index++) {
echo 'test' . PHP_EOL;
sleep(1);
touch($this->log_file_name);
$this->_touchLogFile();
$dateTimeLastTouched = $this->_getDateTimeLogFileLastTouched();
echo $dateTimeLastTouched . PHP_EOL;
}
}
protected function _touchLogFile() {
//touch($this->log_file_name);
exec("touch {$this->log_file_name}");
}
protected function _getDateTimeLogFileLastTouched() {
return filemtime($this->log_file_name);
}
protected function _writeProcessIdToLogFile() {
file_put_contents($this->log_file_name, getmypid());
}
}
$testKeepAlive = new TestKeepAlive();
$testKeepAlive->process();
You should use the function clearstatcache found in the PHP Manual
PHP caches the information those functions(filemtime) return in order
to provide
faster performance. However, in certain cases, you may want to clear the cached
information. For instance, if the same file is being checked multiple times within a
single script, and that file is in danger of being removed or changed during that
script's operation, you may elect to clear the status cache. In these cases, you can
use the clearstatcache() function to clear the information that PHP caches about a file.
Function:
protected function _getDateTimeLogFileLastTouched() {
clearstatcache();
return filemtime($this->log_file_name);
}

Magento - cart page blank caused by the following function (crashing app)

Anyone have any ideas... there's like 15 places this function is getting called, every time it hits it seems to be the problem, it gets through the function but the serialized length of :
echo "Length serialized: " . strlen(serialize($this->_items));
comes out to be about 8000 long.
public function getItemsCollection($useCache = true)
{
if (is_null($this->_items)) {
$this->_items = Mage::getModel('sales/quote_item')->getCollection()
->addFieldToFilter("quote_id", $this->getId())
;
#$this->_items->setQuote($this);
}
echo "Length serialized: " . strlen(serialize($this->_items));
exit;
return $this->_items;
}
Seems to me like there is something weird in the item object. Like one of its members has a large object tied to it. I know that I had issues saving addresses to a session object. Maybe there is something you are doing outside of this that is attaching a large object that errors out. You could do the following to get the raw sql and run it on your database to see what comes back
public function getItemsCollection($useCache = true)
{
if (is_null($this->_items)) {
$this->_items = Mage::getModel('sales/quote_item')->getCollection()
->addFieldToFilter("quote_id", $this->getId())
;
#$this->_items->setQuote($this);
}
echo (String)Mage::getModel('sales/quote_item')->getCollection()->getSelect();
exit;
return $this->_items;
}
i had same issues when i coded with Magento, it's linked to recursion, try var_dump() instead of echo()

Instantiating a AMF PHP class not working

I am trying to use AMF PHP to pass variables to a flash file, thus far I cannot see anything wrong with my code, but I have very little experience with creating classes, so here it goes, here is my code,
index.php:
<?php
include "amfphp/services/flashMe.php";
$session = true;
if ($session == true) {
$uid = '12345';
$thing = new flashMe;
$thing->push($uid);
} else {
//login
}
?>
flashMe.php:
<?php
class flashMe {
public function __construct() {
}
public function push($one)
{
return $one;//sends the uid to the flash file?
}
}
?>
Flash is looking for the flashMe class and the push method within that class, but I keep getting null variables in my flash file when I run it, is there something wrong with this code?
Thanx in advance!
Your index.php file is unnecessary.
Your second file is incomplete. Here is the example from the docs for their "hello world" class file:
<?php
class HelloWorld
{
function HelloWorld()
{
$this->methodTable = array
(
"say" => array
(
"access" => "remote",
"description" => "Pings back a message"
)
);
}
function say($sMessage)
{
return 'You said: ' . $sMessage;
}
}
?>
This file should be saved as "HelloWorld" matching the "class HelloWorld" you have named in the php file (you did this part right with FlashMe).
The example file in the docs for the Flash piece (in actionscript) is here:
import mx.remoting.*;
import mx.rpc.*;
import mx.remoting.debug.NetDebug;
var gatewayUrl:String = "http://localhost/flashservices/gateway.php"
NetDebug.initialize();
var _service:Service = new Service(gatewayUrl, null, 'HelloWorld', null , null);
var pc:PendingCall = _service.say("Hello world!");
pc.responder = new RelayResponder(this, "handleResult", "handleError");
function handleResult(re:ResultEvent)
{
trace('The result is: ' + re.result);
}
function handleError(fe:FaultEvent)
{
trace('There has been an error');
}
The gateway URL should go to wherever your services can be reached. I'm sure if you try a few you'll find the right one. The neat thing about amfphp is that it allows you to also test your services out before you try implementing them in the gateway (if you go to the URL in your browser).
I'm pretty new to AMFPHP as well, but I've found the docs to be extraordinarily useful. If you need more help on classes, you can find more info on the PHP docs page.
You missed the parenthesis after new flashMe
$thing = new flashMe();
$thing->push($uid);
Amfphp or Zend AMF only allow you to call public methods on a remote class that is exposed by your gateway. You example is not a class and therefore no remote method can be called. This looks more like something that you would do with an http post.
http://framework.zend.com/manual/en/zend.amf.server.html

Categories