phpusleep的简单介绍
usleep函数如何影响php语句的执行
在实际应用中是对方调用我的接口请求提交给他上万条记录,而且这个接口他只调用一次,然后我要把这大量记录分段echo输出给他,php自身的定时操作很麻烦,所以我这样开始输出一段——暂停等待传输完——再开始输出下一段!
PHP中使用Memache作为进程锁的操作类分享
?php
//
使用Memache
作为进程锁
class
lock_processlock{
//
key
的前缀
protected
$sLockKeyPre;
//
重试间隔
protected
$iLockRetryInterval;
//重试次数
protected
$iLockRetryCount;
//锁的过期时间
protected
$iLockCacheTimeout;
//
锁过期后的回调函数
protected
$onLockTimeoutFunc;
//
memache
的实例
protected
$oMemcache;
//
存储memcache失败后重试次数
protected
$iMemcacheRetryCount;
public
function
__construct
($onLockTimeoutFunc=NULL)
{
$aLockConfig
=
get_config('',
'lock');
$this-sLockKeyPre
=
self::LOCK_KEY_PRE;
$this-iLockRetryInterval
=
self::LOCK_RETRY_INTERVAL;
$this-iLockRetryCount
=self::LOCK_RETRY_COUNT;
$this-iLockCacheTimeout
=
self::LOCK_CACHE_TIMEOUT;
$this-iMemcacheRetryCount
=
self::LOCK_CACHE_TIMEOUT;
if(!$onLockTimeoutFunc){
//
如果加锁不成功则调用回调函数,如果没有回调函数,使用本类中所带的
$onLockTimeoutFunc
='onLockTimeout'
;
}
$this-onLockTimeoutFunc
=
$onLockTimeoutFunc;
}
/**
连接memcache
*/
public
function
connect()
{
if
(!
isset
(
$this-oMemcache
))
{
$this-oMemcache
=
new
Memcache
();
$this-oMemcache-connect
(
'127.0.0.1',
11211
);
}
return
$this-oMemcache;
}
/*
向MeMcache中添加
key
*/
public
addMemcache($sKey,
$sValue,
$iTimeout){
for($i=
;
$i$this-iMemcacheRetryCount){
$bRes
=
$this-oMemcache-add($sKey,
$sValue,
$iTimeout);
if($bRes){
return
true
;
}
//
如果加锁不成功,sleep
之后,从新加锁
usleep($this-iLockRetryInterval*1000);
}
return
false
;
}
/*
加锁
*/
public
function
lock($sLockID){
$oMemcache
=
$this-connect();
$sKey
=
$this-sLockKeyPre
.
$sLockID;
//
加锁如果不成功可以多试几次
for($i
=
;
$i
$this-iLockRetryCount
;
$i++){
//
这里设置value
的值可以随便设置
if($this-addMemcache($sKey,'1',$this-iLockCacheTimeout)){
return
true
;
}
//
如果加锁不成功,sleep
之后,从新加锁
usleep($this-iLockRetryInterval*1000);
}
//
若还不成功,则加锁失败,调用回调函数,.也就是失败后需要处理的操作
if(is_callable($this-onLockTimeoutFunc)){
//
调用函数
call_user_func($this-onLockTimeoutFunc);
}
}
/*
解锁操作
*/
public
function
unlock($sLockID){
$oMemcache
=
$this-connect();
$sKey
=
$this-sLockKeyPre
.
$sLockID;
//
删除key
return
$this-oMemcache-delete($sKey);
}
/**
如果加锁不成功,则执行如下操作
*/
public
function
onLockTimeout(){
echo
("加锁超时");
}
}
//
应用实例
$oLock
=
new
lock_processlock();
$lockResource
=
"test";
//
加锁
$oLock-lock($lockResource);
//
解锁
$oLock-unlock($lockResource);
php 队列中usleep 会不会导致进程死锁
recv sleep状态,表示 等待网络读取 。 没有必要kiLLsp_who 得出的 blk_spid 即为阻塞的进程ID,也就是处于死锁状态的进程ID
PHP-php 怎么实现类似多线程
php本身不支持多线程,但可以通过curl_multi_*系列函数来模拟多线程,以下是我用过的一个函数,但请求不能过多,否则会出现一些难以解释的问题。希望对你有帮助。
?php
function rolling_curl($urls, $delay) {
$queue = curl_multi_init();
$map = array();
foreach ($urls as $url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOSIGNAL, true);
curl_multi_add_handle($queue, $ch);
$map[(string) $ch] = $url;
}
$responses = array();
do {
while (($code = curl_multi_exec($queue, $active)) == CURLM_CALL_MULTI_PERFORM) ;
if ($code != CURLM_OK) { break; }
// a request was just completed -- find out which one
while ($done = curl_multi_info_read($queue)) {
// get the info and content returned on the request
$info = curl_getinfo($done['handle']);
$error = curl_error($done['handle']);
$results = callback(curl_multi_getcontent($done['handle']), $delay);
$responses[$map[(string) $done['handle']]] = compact('info', 'error', 'results');
// remove the curl handle that just completed
curl_multi_remove_handle($queue, $done['handle']);
curl_close($done['handle']);
}
// Block for data in / output; error handling is done by curl_multi_exec
if ($active 0) {
curl_multi_select($queue, 0.5);
}
} while ($active);
curl_multi_close($queue);
return $responses;
}
function callback($data, $delay) {
preg_match_all('/h3(.+)\/h3/iU', $data, $matches);
usleep($delay);
return compact('data', 'matches');
}
$urls = array();
for($i=0;$i5;$i++)
{
array_push($urls,"");
}
rolling_curl($urls, 0.5);
?
PHP 执行循环时 或者 执行一条语句的时候能不能 指定延迟时间 让它等几秒以后再接着执行操作
如果是秒为单位的话。。sleep 即刻
for($i = 0; $i $10; $i++){
echo $i;
sleep(2); // 暂停2秒
}