php-general Digest 12 Aug 2012 18:47:21 -0000 Issue 7918

Topics (messages 318667 through 318671):

Re: Too many open files
        318667 by: Robert Cummings
        318670 by: Daniel Brown

Re: PHP session variables
        318668 by: Tedd Sperling
        318669 by: Tedd Sperling

Reading class variable value always returns NULL
        318671 by: Reto Kaiser

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
On 12-08-10 02:49 AM, Matijn Woudt wrote:
On Fri, Aug 10, 2012 at 5:36 AM, Jim Lucas <li...@cmsws.com> wrote:
On 8/9/2012 5:01 PM, Al wrote:

Getting "Too many open files" error when processing an email batch
process.

I've looked extensively and can't find more than about 100 files that
could be open.  All my fetching is with get_file_contents();


Why not use fopen() and other related functions to open/grap/close your
batch of files?

You could replace a call like this:

$data = file_get_contents($filename);

with this:

if ( $fh = fopen($filename, 'r') ) {
   $data = fread($fh, filesize($filename));
   fclose($fh);
}

This should take care of your issue.

Jim Lucas

Why on earth would you want to reinvent the wheel? There's no point in
saying that fopen/fread/fclose is better, in fact, let me quote from
the manual page of file_get_contents:
"file_get_contents() is the preferred way to read the contents of a
file into a string. It will use memory mapping techniques if supported
by your OS to enhance performance."

If your solution would fix the problem (I doubt, but ok), then you
should report a bug to the PHP devs that file_get_contents is broken.

It wouldn't fix the problem. Performing fopen/fread/fclose in PHP is slower than the same process implemented in C in the PHP engine. Thus the file will spend more time in the open state thus exacerbating the problem.

Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.

--- End Message ---
--- Begin Message ---
On Fri, Aug 10, 2012 at 10:22 AM, Robert Cummings <rob...@interjinn.com> wrote:
> On 12-08-09 08:01 PM, Al wrote:
>> I can't find a way to see what files could be open or what the limit is.
>>
>> Site is on a shared server, cPanel.
>
>                ^^^^^^^^^^^^^
> THIS is probably your problem. Too many open files indicates that either the
> user OR the OS has reached its limit of allowed open file handles. Open
> files are those used by the OS and every user on the shared server. The
> setting can be changed but you'll need an administrator to increase the
> number of allowed open files. I suspect it's at the OS level if indeed you
> only have 100 files open (though you likely have more due to files opened
> for you by the OS or whatnot.

    Rob is exactly right.  This is managed via the kernel and ulimit,
to prevent excessive resource usage.  Often it's a temporary problem,
but if it consistently occurs, your host may either be improperly
configured or, more likely, overselling resources.

-- 
</Daniel P. Brown>
Network Infrastructure Manager
http://www.php.net/

--- End Message ---
--- Begin Message ---
On Aug 9, 2012, at 5:16 PM, Jim Lucas <li...@cmsws.com> wrote:
> You are relying on PHP's loose typing.  This is a poor check.
> 
> session_id() returns a string, not boolean.
> 
> You should do this instead.
> 
> if ( session_id() === '' )
> ....
> 
> 
> -- 
> Jim Lucas

Thanks Jim -- you're right.

What about?

if (!defined(SID))
        {
        session_start();
        }


Cheers,

tedd

_____________________
t...@sperling.com
http://sperling.com


--- End Message ---
--- Begin Message ---
On Aug 10, 2012, at 11:45 AM, Tedd Sperling <t...@sperling.com> wrote:

> On Aug 9, 2012, at 5:16 PM, Jim Lucas <li...@cmsws.com> wrote:
>> You are relying on PHP's loose typing.  This is a poor check.
>> 
>> session_id() returns a string, not boolean.
>> 
>> You should do this instead.
>> 
>> if ( session_id() === '' )
>> ....
>> 
>> 
>> -- 
>> Jim Lucas
> 
> Thanks Jim -- you're right.
> 
> What about?
> 
> if (!defined(SID))
>       {
>       session_start();
>       }

Before you answer, the "(!defined(SID))" is over 50 times slower than "( 
session_id() === '' )"

Your way is better.

Cheers,

tedd

_____________________
t...@sperling.com
http://sperling.com

--- End Message ---
--- Begin Message ---
Hi,

So I have this strange situation where I assign a classvariable a
value, but when I read the value it is NULL.

The class has one variable declared:
=========
class A {
        private $_cookies;
}
=========

In a method of this class I assign this classvariable plus an
undeclared classvariable and a local variable the value 1:
=========
$this->_cookies = 1;
$this->_cookies2 = 1;
$cookies3 = 1;
=========

When I now read the values of those variables, the classvariables are
NULL while the local variable is 1:
=========
$logEntry .= 'cookies: ' . var_export($this->_cookies, true) . PHP_EOL;
$logEntry .= 'cookies2: ' . var_export($this->_cookies2, true) . PHP_EOL;
$logEntry .= 'cookies3: ' . var_export($cookies3, true) . PHP_EOL;
=========
cookies: NULL
cookies2: NULL
cookies3: 1
=========

But when reading the whole object, the classvariables are 1:
=========
$logEntry .= var_export($this, true) . PHP_EOL;
=========
A::__set_state(array(
   '_cookies' => 1,
   '_cookies2' => 1,
))
=========


This happens periodically on a busy webserver. It seems that when it
happens, all classvariables cannot be read anymore (return NULL).
After restarting Apache it does not happen anymore, just to happen
again after some minutes.

The system is current Debian Squeeze:
Linux: linux-image-2.6.32-5-amd64
Apache: apache2-mpm-prefork 2.2.16-6+squeeze7
PHP: PHP 5.3.3-7+squeeze13 with Suhosin-Patch (cli) (built: Jun 10
2012 07:31:32)
php -m: 
https://raw.github.com/gist/3331641/2f7e80bd03abfb728b659634d3f4bac0131f4d6a/gistfile1.txt
php -i: 
https://raw.github.com/gist/3331651/bcf6e3654bf391482627505447848de173d0bbab/gistfile1.txt

Does anyone have an idea what could cause this, or how to further debug?

Thanks,
 Reto

--- End Message ---

Reply via email to