��ҹ·�ˣ�PHP��ʱ����ȫ���ܽ�
�� ���� ��
��PHP�����й������dz���ʹ�õ���ʱ��������ʱ�ij��ϣ���˵����������
1. �첽��ȡ��������ij����������Դ��ȡ���ɹ�����������Ӱ������ҳ��չ��
2. Ϊ�˱�֤Web������������Ϊ����ҳ�洦�����ܲ�����������������ҳ�棬������ijЩҳ����������
3. ����ijЩ�ϴ����߲�ȷ������ʱ���ij��ϣ�����Ҫ���������������г�ʱ����Ϊ���ޣ������κ�һ���������ò��������ᵼ��Ī��ִ���ж�
4. ��������ģ�飨MySQL��Memcached��HTTP�ӿڣ���Ϊ�˷�ֹ�����ӿ�����̫���������ǰ����ȡ����̫������Ӱ��ҳ�������ٶȣ�����ѩ��
5. �������ܶ���Ҫ��ʱ�ij���
��Щ�ط�����Ҫ���dz�ʱ���趨������PHP�еij�ʱ���Ƿ��ű��࣬����������ʽ�Ͳ��Զ���ͬ��Ϊ��ϵͳ�����������ܽ���PHP�г��õij�ʱ�������ܽᡣ
��Web��������ʱ������
[ Apache ]
һ�������ܸܺߵ������£�ȱʡ���г�ʱ���ö���30�룬�������ϴ��ļ������������ٶȺ����������£���ô���ܴ�����ʱ������
Ŀǰ apache fastcgi php-fpm ģʽ ����������ʱ���ã�
fastcgi ��ʱ���ã�
�� httpd.conf ��fastcgi�������ã��������£�
FastCgiExternalServer /home/forum/apache/apache_php/cgi-bin/php-cgi -socket /home/forum/php5/etc/php-fpm.sock
ScriptAlias /fcgi-bin/ "/home/forum/apache/apache_php/cgi-bin/"
AddHandler php-fastcgi .php
Action php-fastcgi /fcgi-bin/php-cgi
AddType application/x-httpd-php .php
</IfModule>
ȱʡ������ 30s��������Ҫ�����Լ������ã���Ҫ�����ã�������Ϊ100�룺(�ĺ����� apache)��
<IfModule mod_fastcgi.c>
FastCgiExternalServer /home/forum/apache/apache_php/cgi-bin/php-cgi -socket /home/forum/php5/etc/php-fpm.sock -idle-timeout 100
ScriptAlias /fcgi-bin/ "/home/forum/apache/apache_php/cgi-bin/"
AddHandler php-fastcgi .php
Action php-fastcgi /fcgi-bin/php-cgi
AddType application/x-httpd-php .php
</IfModule>
������ʱ�᷵��500���Ͽ�������php���������ӣ�ͬʱ��¼һ��apache������־��
[Thu Jan 27 18:30:15 2011] [error] [client 10.81.41.110] FastCGI: comm with server "/home/forum/apache/apache_php/cgi-bin/php-cgi" aborted: idle timeout (30 sec)
[Thu Jan 27 18:30:15 2011] [error] [client 10.81.41.110] FastCGI: incomplete headers (0 bytes) received from server "/home/forum/apache/apache_php/cgi-bin/php-cgi"
���� fastcgi ���ò���˵����
| IdleTimeout ����ʱ��
ProcessLifeTime һ�����̵���������ڣ�����֮��������kill MaxProcessCount �������̸��� DefaultMinClassProcessCount ÿ��������������С���̸��� DefaultMaxClassProcessCount ÿ�������������������̸��� IPCConnectTimeout ������Ӧ��ʱʱ�� IPCCommTimeout ������ͨѶ���ʱ�䣬�����Ĵ����п��ܾ�������ֵ���ù�С���ɵ� MaxRequestsPerProcess ÿ�������������ɴ������������ɺ���ɱ |
[ Lighttpd ]
���ã�lighttpd.conf
Lighttpd�����У����ڳ�ʱ�IJ��������¼�����ƪ�����ǣ�ֻд����ʱ��д��ʱ����ͬ������
��Ҫ�漰ѡ�
server.max-keep-alive-idle = 5
server.max-read-idle = 60
server.read-timeout = 0
server.max-connection-idle = 360
|
-------------------------------------------------- # ÿ��keep-alive ������������, Ĭ��ֵ��16 server.max-keep-alive-requests = 100
# keep-alive����ȴ�ʱ��, ��λ���룬Ĭ��ֵ��5 server.max-keep-alive-idle = 1200
# lighttpd��work�ӽ�������Ĭ��ֵ��0������������ server.max-worker = 2
# �����û��ڷ��������Ĺ����У��������м�ͣ��ʱ��(��λ����)�� # �����û��ڷ��������Ĺ�����(û��������)���м�ͣ�ٵ�ʱ��̫����lighttpd�������Ͽ����� # Ĭ��ֵ��60(��) server.max-read-idle = 1200
# �����û��ڽ���Ӧ���Ĺ����У��������м�ͣ��ʱ��(��λ����)�� # �����û��ڽ���Ӧ���Ĺ�����(û����)���м�ͣ�ٵ�ʱ��̫����lighttpd�������Ͽ����� # Ĭ��ֵ��360(��) server.max-write-idle = 12000
# ���ͻ��������ij�ʱ���ƣ���λ����, ��Ϊ0��ʾ�������� # ����С��max-read-idleʱ��read-timeout��Ч server.read-timeout = 0
# дӦ��ҳ�����ͻ��˵ij�ʱ���ƣ���λ���룬��Ϊ0��ʾ�������� # ����С��max-write-idleʱ��write-timeout��Ч server.write-timeout = 0
# �����Ĵ���ʱ�����ޣ���������mod_proxy_core���Ǿ��Ǻͺ��˵Ľ���ʱ������, ��λ���� server.max-connection-idle = 1200 -------------------------------------------------- |
˵����
����һ��keep-alive�����ϵ����������͵�һ���������ݵ����������ɲ���max-read-idle�������ӵڶ��������𣬷����������ݵ����������ɲ���max-keep-alive-idle�������������ļ�����ʱҲ��max-keep-alive-idle�����������������ݵ���ʱ�䳬ʱ�ɲ���read-timeout������Lighttpd�����˽������ݵij�ʱ��max-connection-idle������
��������
http://www.snooda.com/read/244
[ Nginx ]
���ã�nginx.conf
|
http { #Fastcgi: (���Ժ��˵�fastcgi ��Ч, fastcgi ������proxyģʽ) fastcgi_connect_timeout 5; #���ӳ�ʱ fastcgi_send_timeout 10; #д��ʱ fastcgi_read_timeout 10; #��ȡ��ʱ
#Proxy: (����proxy/upstreams����Ч) proxy_connect_timeout 15s; #���ӳ�ʱ proxy_read_timeout 24s; #����ʱ proxy_send_timeout 10s; #д��ʱ } |
˵����
Nginx �ij�ʱ���õ��Ƿdz������������⣬���泬ʱ���Բ�ͬ����ģʽ��������Ϊ��ʱ�����������Ƿdz����ġ�
��������
http://hi.baidu.com/pibuchou/blog/item/a1e330dd71fb8a5995ee3753.html
http://hi.baidu.com/pibuchou/blog/item/7cbccff0a3b77dc60b46e024.html
http://hi.baidu.com/pibuchou/blog/item/10a549818f7e4c9df703a626.html
http://www.apoyl.com/?p=466
��PHP������ʱ������
[ PHP-fpm ]
���ã�php-fpm.conf
|
<?xml version="1.0" ?> <configuration> //... Sets the limit on the number of simultaneous requests that will be served. Equivalent to Apache MaxClients directive. Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi Used with any pm_style. #php-cgi�Ľ������� <value name="max_children">128</value>
The timeout (in seconds) for serving a single request after which the worker process will be terminated Should be used when 'max_execution_time' ini option does not stop script execution for some reason '0s' means 'off' #php-fpm ����ִ�г�ʱʱ�䣬0sΪ������ʱ����������һ�� Ns Ϊ��ʱ������ <value name="request_terminate_timeout">0s</value>
The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file '0s' means 'off' <value name="request_slowlog_timeout">0s</value>
</configuration>
|
˵����
�� php.ini �У���һ������ max_execution_time �������� PHP �ű�������ִ��ʱ�䣬���ǣ��� php-cgi(php-fpm) �У��ò���������Ч�������ܹ����� PHP �ű�����ִ��ʱ��
<value name="request_terminate_timeout">0s</value>
����˵������ʹ�� mod_php5.so ��ģʽ���� max_execution_time �ǻ���Ч�ģ�����������php-fpmģʽ������ʱ����Ч�ġ�
��������
http://blog.s135.com/file_get_contents/
[ PHP ]
���ã�php.ini
ѡ�
max_execution_time = 30
�����ڴ��������ã�
ini_set("max_execution_time", 30);
set_time_limit(30);
˵����
�Ե�ǰ�Ự��Ч����������0һֱ����ʱ����������php�� safe_mode �����ˣ���Щ���ö����Ч��
Ч��һ�������Ǿ���������Ҫ�ο�php-fpm�������ݣ�����php-fpm�������� request_terminate_timeout �Ļ�����ô max_execution_time �Ͳ���Ч��
������&�ӿڷ��ʳ�ʱ��
��HTTP���ʡ�
һ�����Ƿ���HTTP��ʽ�ܶ࣬��Ҫ�ǣ�curl, socket, file_get_contents() �ȷ�����
���������Է�������һֱû����Ӧ��ʱ�������Ǿͱ����ˣ������װ����������������������ڷ���http��ʱ��Ҳ��Ҫ���dz�ʱ�����⡣
[ CURL ����HTTP]
CURL �����dz��õ�һ�ֱȽϿ��ķ���HTTPЭ���ӿڵ�lib�⣬���ܸߣ�����һЩ����֧�ֵĹ��ܵȡ�
CURL:
curl_setopt($ch, opt) ��������һЩ��ʱ�����ã���Ҫ������
*(��Ҫ) CURLOPT_TIMEOUT ����cURL����ִ�е��������
*(��Ҫ) CURLOPT_TIMEOUT_MS ����cURL����ִ�е���������� (��cURL 7.16.2�б����롣��PHP 5.2.3����ʹ�á� )
CURLOPT_CONNECTTIMEOUT �ڷ�������ǰ�ȴ���ʱ�䣬��������Ϊ0�������ȴ���
CURLOPT_CONNECTTIMEOUT_MS �������ӵȴ���ʱ�䣬�Ժ���Ϊ��λ����������Ϊ0�������ȴ��� ��cURL 7.16.2�б����롣��PHP 5.2.3��ʼ���á�
CURLOPT_DNS_CACHE_TIMEOUT �������ڴ��б���DNS��Ϣ��ʱ�䣬Ĭ��Ϊ120�롣
curl��ͨ�뼶��ʱ��
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60); //ֻ��Ҫ����һ�����������Ϳ���
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);
curl��ͨ�뼶��ʱʹ�ã�
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl������Ҫ���к��볬ʱ����Ҫ���ӣ�
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
�����ǣ�
curl_setopt ( $ch, CURLOPT_NOSIGNAL, true); �ǿ���֧�ֺ��뼶����ʱ���õ�
curlһ�����뼶��ʱ�����ӣ�
|
<?php if (!isset($_GET['foo'])) { // Client $ch = curl_init('http://example.com/'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_NOSIGNAL, 1); //ע�⣬���볬ʱһ��Ҫ�������� curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200); //��ʱ���룬cURL 7.16.2�б����롣��PHP 5.2.3����ʹ�� $data = curl_exec($ch); $curl_errno = curl_errno($ch); $curl_error = curl_error($ch); curl_close($ch);
if ($curl_errno > 0) { echo "cURL Error ($curl_errno): $curl_error\n"; } else { echo "Data received: $data\n"; } } else { // Server sleep(10); echo "Done."; } ?> |
����һЩ���ɣ�
1. ���վ����ܽ��ǣ�cURL �汾 >= libcurl/7.21.0 �汾�����뼶��ʱ��һ����Ч�ģ��мǡ�
2. curl_multi�ĺ��뼶��ʱҲ�����⡣�����η�����֧��ms����ʱ�ģ�curl_multi���е������
[��������ʽ����HTTP]
����curl�����ǻ������Լ�ʹ��fsockopen��������file��������������HTTPЭ���Ĵ��������ԣ����Ƕ������ij�ʱ����Ҳ�DZ����ġ�
һ�����ӳ�ʱ����ֱ�����ã���������ȡ��ʱ��Ҫ����������
�Լ�д���봦��:
$tmCurrent = gettimeofday();
$intUSGone = ($tmCurrent['sec'] - $tmStart['sec']) * 1000000
+ ($tmCurrent['usec'] - $tmStart['usec']);
if ($intUSGone > $this->_intReadTimeoutUS) {
return false;
}
����ʹ���������������� stream_set_timeout() �� stream_get_meta_data() ������
| <?php
// Timeout in seconds $timeout = 5; $fp = fsockopen("example.com", 80, $errno, $errstr, $timeout); if ($fp) { fwrite($fp, "GET / HTTP/1.0\r\n"); fwrite($fp, "Host: example.com\r\n"); fwrite($fp, "Connection: Close\r\n\r\n"); stream_set_blocking($fp, true); //��Ҫ������Ϊ������ģʽ stream_set_timeout($fp,$timeout); //���ó�ʱ $info = stream_get_meta_data($fp); while ((!feof($fp)) && (!$info['timed_out'])) { $data .= fgets($fp, 4096); $info = stream_get_meta_data($fp); ob_flush; flush(); } if ($info['timed_out']) { echo "Connection Timed Out!"; } else { echo $data; } } |
file_get_contents ��ʱ��
| <?php
$timeout = array( 'http' => array( 'timeout' => 5 //����һ����ʱʱ�䣬��λΪ�� ) ); $ctx = stream_context_create($timeout); $text = file_get_contents("http://example.com/", 0, $ctx); ?> |
fopen ��ʱ��
| <?php
$timeout = array( 'http' => array( 'timeout' => 5 //����һ����ʱʱ�䣬��λΪ�� ) ); $ctx = stream_context_create($timeout); if ($fp = fopen("http://example.com/", "r", false, $ctx)) { while( $c = fread($fp, 8192)) { echo $c; } fclose($fp); } ?> |
��MySQL��
php�е�mysql�ͻ��˶�û�����ó�ʱ��ѡ�mysqli��mysql��û�У�����libmysql���ṩ��ʱѡ���ģ�ֻ��������php�������˶��ѡ�
��ô������PHP��ʹ��������������Ҫ�����Լ�����һЩMySQL������������Ҫ�漰�ij����У�
MYSQL_OPT_READ_TIMEOUT=11;
MYSQL_OPT_WRITE_TIMEOUT=12;
�������������Ժ���ʹ�� options ������Ӧ��ֵ��
�����и�ע���㣬mysql�ڲ�ʵ�֣�
1. ��ʱ���õ�λΪ�룬��������1��
2. ��mysql�ײ���read���������Σ�����ʵ�ʻ��� 3 ��
�������� +������һ�� = 3����ʱʱ�䣬��ô����˵���ٳ�ʱʱ����3�룬������������ֵ�����ڴ�Ӧ����˵���Խ��ܣ����Ƕ���С����Ӧ����Ҫ�Ż���
�鿴һ�����÷���mysql��ʱ��phpʵ����
| <?php
//�Լ�������д��ʱ���� if (!defined('MYSQL_OPT_READ_TIMEOUT')) { define('MYSQL_OPT_READ_TIMEOUT', 11); } if (!defined('MYSQL_OPT_WRITE_TIMEOUT')) { define('MYSQL_OPT_WRITE_TIMEOUT', 12); } //���ó�ʱ $mysqli = mysqli_init(); $mysqli->options(MYSQL_OPT_READ_TIMEOUT, 3); $mysqli->options(MYSQL_OPT_WRITE_TIMEOUT, 1);
//�������ݿ� $mysqli->real_connect("localhost", "root", "root", "test"); if (mysqli_connect_errno()) { printf("Connect failed: %s/n", mysqli_connect_error()); exit(); } //ִ�в�ѯ sleep 1�벻��ʱ printf("Host information: %s/n", $mysqli->host_info); if (!($res=$mysqli->query('select sleep(1)'))) { echo "query1 error: ". $mysqli->error ."/n"; } else { echo "Query1: query success/n"; } //ִ�в�ѯ sleep 9���ᳬʱ if (!($res=$mysqli->query('select sleep(9)'))) { echo "query2 error: ". $mysqli->error ."/n"; } else { echo "Query2: query success/n"; } $mysqli->close(); echo "close mysql connection/n"; ?> |
��������
http://blog.csdn.net/heiyeshuwu/article/details/5869813
��Memcached��
[PHP��չ]
php_memcache �ͻ��ˣ�
���ӳ�ʱ��bool Memcache::connect ( string $host [, int $port [, int $timeout ]] )
��get��set��ʱ��û����ȷ�ij�ʱ���ò�����
libmemcached �ͻ��ˣ���php�ӿ�û�����Եij�ʱ������
˵��������˵����PHP�з���Memcached�Ǵ��ںܶ������ģ���Ҫ�Լ�hack���ֲ����������Dzο����ϲ�����
[C&C++����Memcached]
�ͻ��ˣ�libmemcached �ͻ���
˵����memcache��ʱ���ÿ�������С�㣬����5��10�������Ѿ������ˣ���������ʱ�仹���������ݿ���ѯ��
������һ�����ӺͶ�ȡset���ݵij�ʱ��C++ʾ����
|
//�������ӳ�ʱ�����ӵ�Memcached�� memcached_st* MemCacheProxy::_create_handle() { memcached_st * mmc = NULL; memcached_return_t prc; if (_mpool != NULL) { // get from pool mmc = memcached_pool_pop(_mpool, false, &prc); if (mmc == NULL) { __LOG_WARNING__("MemCacheProxy", "get handle from pool error [%d]", (int)prc); } return mmc; }
memcached_st* handle = memcached_create(NULL); if (handle == NULL){ __LOG_WARNING__("MemCacheProxy", "create_handle error"); return NULL; }
// ��������/��ȡ��ʱ memcached_behavior_set(handle, MEMCACHED_BEHAVIOR_HASH, MEMCACHED_HASH_DEFAULT); memcached_behavior_set(handle, MEMCACHED_BEHAVIOR_NO_BLOCK, _noblock); //����MEMCACHED_BEHAVIOR_NO_BLOCKΪ1ʹ��ʱ������Ч�������ó�ʱ���Ч���ؼ�ʱ���ᱯ���ģ���������ѩ�� memcached_behavior_set(handle, MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, _connect_timeout); //���ӳ�ʱ memcached_behavior_set(handle, MEMCACHED_BEHAVIOR_RCV_TIMEOUT, _read_timeout); //����ʱ memcached_behavior_set(handle, MEMCACHED_BEHAVIOR_SND_TIMEOUT, _send_timeout); //д��ʱ memcached_behavior_set(handle, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, _poll_timeout);
// ����һ��hash // memcached_behavior_set_distribution(handle, MEMCACHED_DISTRIBUTION_CONSISTENT); memcached_behavior_set(handle, MEMCACHED_BEHAVIOR_DISTRIBUTION, MEMCACHED_DISTRIBUTION_CONSISTENT);
memcached_return rc; for (uint i = 0; i < _server_count; i++){ rc = memcached_server_add(handle, _ips[i], _ports[i]); if (MEMCACHED_SUCCESS != rc) { __LOG_WARNING__("MemCacheProxy", "add server [%s:%d] failed.", _ips[i], _ports[i]); } }
_mpool = memcached_pool_create(handle, _min_connect, _max_connect); if (_mpool == NULL){ __LOG_WARNING__("MemCacheProxy", "create_pool error"); return NULL; }
mmc = memcached_pool_pop(_mpool, false, &prc); if (mmc == NULL) { __LOG_WARNING__("MyMemCacheProxy", "get handle from pool error [%d]", (int)prc); } //__LOG_DEBUG__("MemCacheProxy", "get handle [%p]", handle); return mmc; }
//����һ��key��ʱ��setһ�����ݵ�memcached�� bool MemCacheProxy::_add(memcached_st* handle, unsigned int* key, const char* value, int len, unsigned int timeout) { memcached_return rc;
char tmp[1024]; snprintf(tmp, sizeof (tmp), "%u#%u", key[0], key[1]); //�и�timeoutֵ rc = memcached_set(handle, tmp, strlen(tmp), (char*)value, len, timeout, 0); if (MEMCACHED_SUCCESS != rc){ return false; } return true; } |
//Memcache��ȡ���ݳ�ʱ (û������)
libmemcahed Դ���нӿڶ��壺
LIBMEMCACHED_API char *memcached_get(memcached_st *ptr,const char *key, size_t key_length,size_t *value_length,uint32_t *flags,memcached_return_t *error);
LIBMEMCACHED_API memcached_return_t memcached_mget(memcached_st *ptr,const char * const *keys,const size_t *key_length,size_t number_of_keys);
�ӽӿ��п��Կ����ڶ�ȡ���ݵ�ʱ������û�г�ʱ���õġ�
��������
http://hi.baidu.com/chinauser/item/b30af90b23335dde73e67608
http://libmemcached.org/libMemcached.html
������ʵ�ֳ�ʱ��
��������Ҫ�г�ʱ���ֹ��ܣ������㵥������һ������Socketģ�飬Socketģ�鲻�������������������κ�һ�ֵ�ʱ��������Э��Ҳ��˽�еģ���ô����ʱ��������Ҫ�Լ�ȥʵ��һЩ��ʱ�������ԣ�����ʱ������ҪһЩ���������ˡ�
[PHP�г�ʱʵ��]
һ�����������ij�ʱʵ�� ���뼶��ʱ��
˼·�ܼ�����һ�����ˣ�Ȼ������Ϊ������ģʽ������û�������Ͼ�һֱѭ�����жϵ�ǰʱ���ͳ�ʱʱ��֮���IJ��졣
php socket ��ʵ��ԭʼ�ij�ʱ��(ÿ��ѭ������ǰʱ��ȥ�������ܻ��ܲcpuռ�û��ϸ�)
| <?
$host = "127.0.0.1"; $port = "80"; $timeout = 15; //timeout in seconds
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Unable to create socket\n");
socket_set_nonblock($socket) //��������Ϊ����ģʽ or die("Unable to set nonblock on socket\n");
$time = time(); //ѭ����ʱ��ÿ�ζ���ȥ��Ӧֵ while (!@socket_connect($socket, $host, $port)) //����û�������Ͼ�һֱ��ѭ�� { $err = socket_last_error($socket); if ($err == 115 || $err == 114) { if ((time() - $time) >= $timeout) //ÿ�ζ���Ҫȥ�ж�һ���Ƿ���ʱ�� { socket_close($socket); die("Connection timed out.\n"); } sleep(1); continue; } die(socket_strerror($err) . "\n"); } socket_set_block($this->socket) //��ԭ����ģʽ or die("Unable to set block on socket\n"); ?> |
����������ʹ��PHP�Դ��첽IOȥʵ�֣����뼶��ʱ��
˵����
�첽IO���첽IO�ĸ�����ͬ��IO���ԡ���һ���첽���̵��÷��������߲������̵õ�������ʵ�ʴ����������õIJ��������ɺ���ͨ��״̬��֪ͨ�ͻص���֪ͨ�����ߡ��첽IO�����طֳ�С�����д��ͣ�С��������8λ��1���ַ������������ͷ��������κ�ʱ�̷�����Щ�����飬�����շ��Ӳ�֪�����ǻ���ʲôʱ�
��·���ã�����ģ���ǶԶ���IO�������м��⣬���ؿɲ������ϣ������Ϳ��Զ������в����ˡ������ͱ���������IO������ʱ��������IO�ͷ�����ռ��ϵͳ��Դ��ȷ����
ʹ�� socket_select() ʵ�ֳ�ʱ
socket_select(..., floor($timeout), ceil($timeout*1000000));
select���ص㣺�ܹ����õ��뼶���ij�ʱ��
ʹ��socket_select() �ij�ʱ��������Ҫ�˽�һЩ�첽IO���̵�֪ʶȥ���⣩
|
### ������ #### <?php $server = new Server; $client = new Client;
for (;;) { foreach ($select->can_read(0) as $socket) {
if ($socket == $client->socket) { // New Client Socket $select->add(socket_accept($client->socket)); } else { //there's something to read on $socket } } } ?>
### �첽��·����IO & ��ʱ���Ӵ����� ### <?php class select { var $sockets;
function select($sockets) {
$this->sockets = array();
foreach ($sockets as $socket) { $this->add($socket); } }
function add($add_socket) { array_push($this->sockets,$add_socket); }
function remove($remove_socket) { $sockets = array();
foreach ($this->sockets as $socket) { if($remove_socket != $socket) $sockets[] = $socket; }
$this->sockets = $sockets; }
function can_read($timeout) { $read = $this->sockets; socket_select($read,$write = NULL,$except = NULL,$timeout); return $read; }
function can_write($timeout) { $write = $this->sockets; socket_select($read = NULL,$write,$except = NULL,$timeout); return $write; } } ?> |
[C&C++�г�ʱʵ��]
һ����Linux C/C++�У�����ʹ�ã�alarm() ���ö�ʱ���ķ�ʽʵ���뼶��ʱ�����ߣ�select()��poll()��epoll() ֮�����첽����IOʵ�ֺ��뼶��ʱ��Ҳ����ʹ�ö��η�װ���첽io�⣨libevent, libev��Ҳ��ʵ�֡�
һ��ʹ��alarm�����ź�ʵ�ֳ�ʱ ���뼶��ʱ��
˵����Linux�ں�connect��ʱͨ��Ϊ75�룬���ǿ������ø�С��ʱ����10������ǰ��connect�з��ء�������ʹ���źŴ������ƣ�����alarm����ʱ������SIGALRM�ź� ��Ҳ��ʹ��selectʵ�֣�
�� alarym �뼶ʵ�֡�connect ���ó�ʱ����ʾ����
| //�źŴ�������
static void connect_alarm(int signo) { debug_printf("SignalHandler"); return; }
//alarm��ʱ����ʵ�� static void conn_alarm() { ����Sigfunc * sigfunc ; //�����źŴ������� ����sigfunc=signal(SIGALRM, connect_alarm); //�����źŴ�������connect_alarm,(������)�������е��źŴ������� int timeout = 5;
//�������� ����if( alarm(timeout)!=0 ){ ���� //... �����Ѿ����ô��� ����}
//�������Ӳ��� if (connect(m_Socket, (struct sockaddr *)&addr, sizeof(addr)) < 0 ) { if ( errno == EINTR ) { //��������������ΪEINTR��˵����ʱ�ж��� debug_printf("Timeout"); m_connectionStatus = STATUS_CLOSED; errno = ETIMEDOUT; //��ֹ�������ּ������� return ERR_TIMEOUT; } else { debug_printf("Other Err"); m_connectionStatus = STATUS_CLOSED; return ERR_NET_SOCKET; } } ����alarm(0);//�ر�ʱ�� ����signal(SIGALRM, sigfunc); //(������)�ָ�ԭ�����źŴ������� ����return; } |
//��ȡ���ݵij�ʱ����
ͬ������Ϊ recv ���ó�ʱ��5�����ղ����κ�Ӧ�����ж�
signal( ... );
alarm(5);
recv( ... );
alarm(0);
static void sig_alarm(int signo){return;}
���ͻ��������ڶ�(readline,...)ʱ��������ʱ���������ˣ��ͻ�TCP��ͼ�ӷ���������һ��ACK�������ش� ���ݷֽڣ���ԼҪ��9���Ӳŷ����ش���������һ�����������ˣ��ڿͻ�������ʱ�����ó�ʱ��
����ʹ���첽����IOʹ�� �����뼶��ʱ��
�첽IOִ�����̣�
1.���Ƚ���־λ��ΪNon-blockingģʽ�����ڷ�����ģʽ�µ���connect����
2.����connect�����������£���ΪTCP����������ҪһЩʱ�䣻������������ֻҪ�����������ɾͻ᷵�ش��������������᷵��EINPROGRESS����ʾ�ڽ������ӵ���û�����ɡ�
3.�ڶ��ӿ���������(fd_set rset)��д�ӿ���������(fd_set wset)�н���ǰ�ӿ���λ����FD_ZERO()��FD_SET()�꣩�������úó�ʱʱ��(struct timeval *timeout)
4.����select( socket, &rset, &wset, NULL, timeout )
����0��ʾconnect��ʱ�����������õij�ʱʱ������75����û�б�Ҫ�������ˣ���Ϊ�ں��ж�connect�г�ʱ���ƾ���75�롣
//select ʵ�ֺ��뼶��ʱʾ����
|
static void conn_select() { // Open TCP Socket m_Socket = socket(PF_INET,SOCK_STREAM,0); if( m_Socket < 0 ) { m_connectionStatus = STATUS_CLOSED; return ERR_NET_SOCKET; }
struct sockaddr_in addr; inet_aton(m_Host.c_str(), &addr.sin_addr); addr.sin_port = htons(m_Port); addr.sin_family = PF_INET;
// Set timeout values for socket struct timeval timeouts; timeouts.tv_sec = SOCKET_TIMEOUT_SEC ; // const -> 5 timeouts.tv_usec = SOCKET_TIMEOUT_USEC ; // const -> 0 uint8_t optlen = sizeof(timeouts);
if( setsockopt( m_Socket, SOL_SOCKET, SO_RCVTIMEO,&timeouts,(socklen_t)optlen) < 0 ) { m_connectionStatus = STATUS_CLOSED; return ERR_NET_SOCKET; }
// Set the Socket to TCP Nodelay ( Send immediatly after a send / write command ) int flag_TCP_nodelay = 1; if ( (setsockopt( m_Socket, IPPROTO_TCP, TCP_NODELAY, (char *)&flag_TCP_nodelay, sizeof(flag_TCP_nodelay))) < 0) { m_connectionStatus = STATUS_CLOSED; return ERR_NET_SOCKET; } // Save Socket Flags int opts_blocking = fcntl(m_Socket, F_GETFL); if ( opts_blocking < 0 ) { return ERR_NET_SOCKET; } //����Ϊ������ģʽ int opts_noblocking = (opts_blocking | O_NONBLOCK); // Set Socket to Non-Blocking if (fcntl(m_Socket, F_SETFL, opts_noblocking)<0) { return ERR_NET_SOCKET; } // Connect if ( connect(m_Socket, (struct sockaddr *)&addr, sizeof(addr)) < 0) { // EINPROGRESS always appears on Non Blocking connect if ( errno != EINPROGRESS ) { m_connectionStatus = STATUS_CLOSED; return ERR_NET_SOCKET; } // Create a set of sockets for select fd_set socks; FD_ZERO(&socks); FD_SET(m_Socket,&socks); // Wait for connection or timeout int fdcnt = select(m_Socket+1,NULL,&socks,NULL,&timeouts); if ( fdcnt < 0 ) { return ERR_NET_SOCKET; } else if ( fdcnt == 0 ) { return ERR_TIMEOUT; } } //Set Socket to Blocking again if(fcntl(m_Socket,F_SETFL,opts_blocking)<0) { return ERR_NET_SOCKET; }
m_connectionStatus = STATUS_OPEN; return 0; } |
˵�����ڳ�ʱʵ�ַ��棬������ʲô�ű����ԣ�PHP��Python��Perl �����ײ㶼��C&C++����Щʵ�ַ�ʽ����Ҫ������Щ��ʱ��������ҪһЩLinux ���̺��������̵�֪ʶ��
��������
http://blog.sina.com.cn/s/blog_4462f8560100tvgo.html
http://blog.csdn.net/thimin/article/details/1530839
http://hi.baidu.com/xjtdy888/item/93d9daefcc1d31d1ea34c992
http://blog.csdn.net/byxdaz/article/details/5461142
http://blog.163.com/xychenbaihu@yeah/blog/static/13222965520112163171778/
http://hi.baidu.com/suyupin/item/df10004decb620e91f19bcf5
http://stackoverflow.com/questions/7092633/connect-timeout-with-alarm
http://stackoverflow.com/questions/7089128/linux-tcp-connect-with-select-fails-at-testserver?lq=1
http://cppentry.com/bencandy.php?fid=54&id=1129
�� �ܽ� ��
1. PHPӦ�ò��������ó�ʱ?
PHP�ڴ�����ʱ�����кܶ࣬��ͬ���Σ���Ҫǰ�˰��ݺ��˳�ʱ��
���������ͻ��ˣ� -> ������ -> Web������ -> PHP -> ���� (MySQL��Memcached��
����˵�������㣨Web�������㣩�ij�ʱʱ����������PHP��PHP-FPM�������õij�ʱʱ�䣬��Ȼ����û�����꣬��ǰ���ͳ�ʱ�ر��ˣ��������ܱ��ߡ����о���PHP�ij�ʱʱ��Ҫ����PHP�������ʺ��ˣ�MySQL��HTTP��Memcached���ij�ʱʱ�䣬��Ȼ����ͬǰ�档
2. ��ʱ����ԭ����ʲô��
������ϣ�����ò���ʱ�Ĵ��루�����ϴ������߶����ܵij�������Ȼ��������һ����ʱʱ�䣬����12��Сʱ�����ģ���Ҫ��Ϊ�˱�֤�������ú�סһ��php���̻��ߺ��ˣ�������������ҳ���ṩ�����������������л���ѩ����
������ҪҪ��������Ӧ�ij��������˳�ʱ���ö�һЩ����������500ms����1s��д1s���������ٶȣ������ܹ������ȼ���Ӧ��ѩ�������⣬�����÷���������̫�ߡ�
3. �Լ�������ʱ���ʺ�������
һ�������������ѣ����������кܶ��������̿���Ҳ�á�������Ҳ�ã�����һ�㶼���г�ʱ��ʵ�֣�����һЩ����IO��lib�⣬����ʹ���������õģ��Լ��ظ�������������bug��Ҳ������ά�������������ǻ���ѧϰ��Ŀ�ľ͵������ˣ���
4. ��������
��ʱ������Ӧ���ﶼ�Ǵ����⣬�ڿ���Ӧ�õ�ʱ����Ҫ���ǵ����Ҽ���һЩӦ�ó�ʱ�����ϰ����ģ��������ܾ�ίʵ���ˣ��Ҿٸ����ӣ�
������php-fpm����128��php-cgi���̣�Ȼ�����ij�ʱ���õ���32s����ô�����������˷����Ƚϲ���������£���ô����ÿ������Ӧ�������ǣ�
128 / 32 = 4��
��û������1��ֻ�ܴ���4���������Ƿ���Ҳ̫���ˣ���Ȼ���ǿ���php-cgi���̿������ڴ�ռ�ã����н���֮���л��ɱ�Ҳ�����ӣ�cpuѽ���ڴ�ѽ�������ӣ�����Ҳ��ȶ������ԣ���������һ�������ij�ʱֵ�����߶��ٺ����������ܡ�
�����в��־���ֵ�����в��ֲο������ݣ���������֮��������ָ����
���ߣ�heiyeluren
���ͣ�http://blog.csdn.net/heiyeshuwu
ʱ�䣺2012/8/8
ԭ�ģ�http://blog.csdn.net/heiyeshuwu/article/details/7841366
- ת����ע����Դ��ITѧϰ�� ��ַ��http://www.t086.com/ �����������Ƽ�������
- �ر������� ��վ�������ر�������ֹת�ص�ר�������������¿�������ת�أ���������ע��������ԭʼ���ߡ����°�Ȩ������ԭʼ�������С����ڱ���վת�����µĸ��˺���վ�����DZ�ʾ������л�⡣������վת�ص������а�Ȩ��������ϵ���ǣ����ǻᾡ�����Ը�����
- ��һƪ���γ�:ǰ�˿���ʮ��̸��һ��
- ��һƪ��PNG����
- �鿴2012��08�µ����¹鵵
- ������������������̸�����ɶ��ر�
- ��������������ѡʳƷ����2020�ļ��ȵ�Ͷ�ʲ��Ժ���������
- �����ڵ�����֤50���ƴ�50����ָ֤�������Ĺ���
- ����֤50����Щ��Ʊ_��֤50Ȩ����������
- ������300����֤50����֤500��ָ��2019���ڶ��ζ��ڵ���������
- ������300��ֵ/�ɳ�ָ�����Ʒ����������й�ƽ��Ȩ���½�
- ����ָ֤��������֤50��ָ��������
- ����ָ֤��������֤����ָ��������
- ����ָ֤��������֤100ָ��������
- ����ָ֤����������300ָ��������