本文整理汇总了PHP中usleep函数的典型用法代码示例。如果您正苦于以下问题:PHP usleep函数的具体用法?PHP usleep怎么用?PHP usleep使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了usleep函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: save
public function save($str)
{
if (!$this->logFilename) {
return false;
}
$hash = md5($str);
// if we've already logged this during this session, then don't do it again
if (in_array($hash, $this->itemsLogged)) {
return true;
}
$ts = date("Y-m-d H:i:s");
$str = $this->cleanStr($str);
if ($fp = fopen($this->logFilename, "a")) {
$trys = 0;
$stop = false;
while (!$stop) {
if (flock($fp, LOCK_EX)) {
fwrite($fp, "{$ts}{$this->delimeter}{$str}\n");
flock($fp, LOCK_UN);
$this->itemsLogged[] = $hash;
$stop = true;
} else {
usleep(2000);
if ($trys++ > 20) {
$stop = true;
}
}
}
fclose($fp);
return true;
} else {
return false;
}
}
示例2: lock
/**
* Lock the resource
*
* @param bool $blocking wait until the lock is released
* @return bool Returns true if the lock was acquired, false otherwise
* @throws IOException If the lock file could not be created or opened
*/
public function lock($blocking = false)
{
if ($this->handle) {
return true;
}
// Silence both userland and native PHP error handlers
$errorLevel = error_reporting(0);
set_error_handler('var_dump', 0);
if (!($this->handle = fopen($this->file, 'r'))) {
if ($this->handle = fopen($this->file, 'x')) {
chmod($this->file, 0444);
} elseif (!($this->handle = fopen($this->file, 'r'))) {
usleep(100);
// Give some time for chmod() to complete
$this->handle = fopen($this->file, 'r');
}
}
restore_error_handler();
error_reporting($errorLevel);
if (!$this->handle) {
$error = error_get_last();
throw new IOException($error['message'], 0, null, $this->file);
}
// On Windows, even if PHP doc says the contrary, LOCK_NB works, see
// https://bugs.php.net/54129
if (!flock($this->handle, LOCK_EX | ($blocking ? 0 : LOCK_NB))) {
fclose($this->handle);
$this->handle = null;
return false;
}
return true;
}
示例3: gpio
function gpio($ettings, $target)
{
$tatus = readGPIO($target);
$gpio = $target['gpioNumber'];
//read in cmd flag if set
$cmd = isset($_GET["cmd"]) ? $_GET["cmd"] : null;
//test if value is a number
if (is_numeric($target['gpioNumber'])) {
//set the gpio's mode to output
setMode($target, "out");
//toggle the gpio to high/low
$tatus = $tatus == "0" ? 1 : 0;
//check for commanded status flag and act upon it.
if (isset($cmd)) {
$tatus = $cmd == "off" ? 0 : 1;
}
writeGPIO($target, $tatus);
//reading pin's status
$status = readGPIO($target);
echo $status;
writeTimeStamp();
//only wait to change state if default state is not current state
if ($target['state'] != readGPIO($target)) {
if (isset($target['timer']) && $target['timer'] > 0) {
usleep($target['timer'] * 1000000);
writeDefaultToGPIO($target);
}
}
} else {
echo "fail";
}
writeTimeStamp();
}
示例4: execute
protected function execute(InputInterface $input, OutputInterface $output)
{
$projectUuid = $input->getArgument('project-uuid');
$api = $this->getApplication()->getApi();
$analysis = $api->analyze($projectUuid, $input->getOption('reference'));
$chars = array('-', '\\', '|', '/');
$position = 0;
while (true) {
// we don't check the status too often
if (0 == $position % 2) {
$analysis = $api->getAnalysisStatus($projectUuid, $analysis->getNumber());
}
if ('txt' === $input->getOption('format')) {
$output->write(sprintf("%s %-80s\r", $chars[$position % 4], $analysis->getStatusMessage()));
}
if ($analysis->isFinished()) {
break;
}
usleep(200000);
++$position;
}
$analysis = $api->getAnalysis($projectUuid, $analysis->getNumber());
if ($analysis->isFailed()) {
$output->writeln(sprintf('There was an error: "%s"', $analysis->getFailureMessage()));
return 1;
}
$helper = new DescriptorHelper($api->getSerializer());
$helper->describe($output, $analysis, $input->getOption('format'), $input->getOption('show-ignored-violations'));
if ('txt' === $input->getOption('format') && OutputInterface::VERBOSITY_VERBOSE > $output->getVerbosity()) {
$output->writeln('');
$output->writeln(sprintf('Run <comment>%s %s %s -v</comment> to get the full report', $_SERVER['PHP_SELF'], 'analysis', $projectUuid));
}
}
示例5: test
public function test()
{
$i = (int) $this->input->get('i');
@usleep(1000 - i * 250);
$this->output->set_header('Content-type: text/html; charset=utf-8', true);
$this->output->set_output('result ' . $i);
}
示例6: sendWPRCRequestWithRetries
/**
* Send request to the WPRC server only with retries
*
* @param string method
* @param mixed arguments to send
*/
public function sendWPRCRequestWithRetries($method, $args, $timeout = 5)
{
$url = WPRC_SERVER_URL;
$send_result = false;
$failed = 0;
$timer = get_transient('wprc_report_failed_timer');
if ($timer != false && $timer != '' && $timer != null) {
$timer = intval($timer);
} else {
$timer = 0;
}
$timenow = time();
if ($timer - $timenow > 0) {
return false;
}
// discard report
while ($send_result === false && $failed < 2) {
$send_result = $this->sendRequest($method, $url, $args, $timeout);
if ($send_result === false) {
$failed++;
if ($failed < 2) {
usleep(rand(100, 300));
}
// wait 1 to 3 seconds
}
}
if ($send_result === false) {
set_transient('wprc_report_failed_timer', time() + 5 * 60 * 60);
} else {
// reset flags
set_transient('wprc_report_failed_timer', 0);
}
return $send_result;
}
示例7: lock
/**
* 上锁
*
* @param string $key
* @param bool $wouldblock 是否堵塞
*
* @return mixed
*/
public function lock($key, $wouldblock = false)
{
if (empty($key)) {
return false;
}
if (isset(self::$lockCache[$key])) {
//FileLock不支持设置过期时间
return true;
}
$fileName = $this->getFileName($key);
if (!($fp = fopen($fileName, 'w+'))) {
return false;
}
if (flock($fp, LOCK_EX | LOCK_NB)) {
self::$lockCache[$fileName] = $fp;
return true;
}
//非堵塞模式
if (!$wouldblock) {
return false;
}
//堵塞模式
do {
usleep(200);
} while (!flock($fp, LOCK_EX | LOCK_NB));
self::$lockCache[$fileName] = $fp;
return true;
}
示例8: login
/**
* Handle connecting and logging into Magrathea. Returns 0 on success.
* @param $hostname The hostname to connect to
* @param $username Username
* @param $password Password
*/
function login($hostname, $username, $password)
{
$r = "";
$code = "";
$msg = "";
$ret = $this->connect($hostname, $this->port, $username, $password);
if ($ret == 0) {
$ret = false;
$this->getresponse($r);
if ($this->use_usleep) {
usleep($this->sleeptime);
} else {
sleep(1);
}
$this->loginprompt = $r;
$this->docommand("AUTH {$username} {$password}", $r);
if ($this->parse_response($r, $code, $msg)) {
if ($code == 0) {
# Logged in!
$ret = true;
}
}
} else {
$ret = false;
}
return $ret;
}
示例9: grabData
public function grabData($url)
{
$pageNum = 2;
$nextUrl = $url;
do {
$content = $this->getContent($nextUrl, 'windows-1251');
$saw = $this->getNokogiri($content);
$nextUrl = $saw->get('a.next')->toArray();
if (array_key_exists('0', $nextUrl)) {
$nextUrl = $nextUrl[0]['href'];
$nextUrl = preg_replace("#page_\\d+\\.#uis", "page_{$pageNum}.", $nextUrl);
if (strpos($content, "page_{$pageNum}.html") === false) {
$nextUrl = null;
}
} else {
$nextUrl = null;
}
$pageNum++;
$detailInfoUrl = $this->grabList($content);
foreach ($detailInfoUrl as $detailUrl) {
echo "Item: {$detailUrl}\n";
$content = $this->getContent($detailUrl, 'windows-1251');
$this->grabItem($detailUrl, $content);
}
echo "Dir: {$nextUrl}\n";
usleep(500000);
} while ($nextUrl);
}
示例10: pulse
public static function pulse($pin, $miliseconds, $state)
{
Gpio::write($pin, $state);
usleep($miliseconds);
$state = $state == 1 ? 0 : 1;
Gpio::write($pin, $state);
}
示例11: write
/**
* @return SocketOutputStream
**/
public function write($buffer)
{
if ($buffer === null) {
return $this;
}
$totalBytes = strlen($buffer);
try {
$writtenBytes = $this->socket->write($buffer);
if ($writtenBytes === false) {
throw new IOTimedOutException('writing to socket timed out');
}
$i = 0;
while ($writtenBytes < $totalBytes && $i < self::WRITE_ATTEMPTS) {
// 0.1s sleep insurance if something wrong with socket
usleep(100000);
$remainingBuffer = substr($buffer, $writtenBytes);
// NOTE: ignoring timeouts here
$writtenBytes += $this->socket->write($remainingBuffer);
++$i;
}
} catch (NetworkException $e) {
throw new IOException($e->getMessage());
}
if ($writtenBytes < $totalBytes) {
throw new IOException('connection is too slow or buffer is too large?');
}
return $this;
}
示例12: uploadAction
public function uploadAction()
{
$this->_helper->viewRenderer->setNoRender(true);
$this->view->layout()->disableLayout(true);
$adapter = new Zend_ProgressBar_Adapter_JsPush(array('updateMethodName' => 'Zend_ProgressBar_Update', 'finishMethodName' => 'Zend_ProgressBar_Finish'));
$progressBar = new Zend_ProgressBar($adapter, 0, 100);
for ($i = 1; $i <= 100; $i++) {
if ($i < 20) {
$text = 'Just beginning';
} else {
if ($i < 50) {
$text = 'A bit done';
} else {
if ($i < 80) {
$text = 'Getting closer';
} else {
$text = 'Nearly done';
}
}
}
$progressBar->update($i, $text);
usleep(100000);
}
$progressBar->finish();
}
示例13: wait_reply
private function wait_reply($expected_result, $timeout)
{
$this->debugmsg("Waiting {$timeout} seconds for expected result");
//Clear buffer
$this->buffer = '';
//Set timeout
$timeoutat = time() + $timeout;
//Loop until timeout reached (or expected result found)
do {
$this->debugmsg('Now: ' . time() . ", Timeout at: {$timeoutat}");
$buffer = fread($this->fp, 1024);
$this->buffer .= $buffer;
usleep(200000);
//0.2 sec
$this->debugmsg("Received: {$buffer}");
//Check if received expected responce
if (preg_match('/' . preg_quote($expected_result, '/') . '$/', $this->buffer)) {
$this->debugmsg('Found match');
return true;
//break;
} else {
if (preg_match('/\\+CMS ERROR\\:\\ \\d{1,3}\\r\\n$/', $this->buffer)) {
return false;
}
}
} while ($timeoutat > time());
$this->debugmsg('Timed out');
return false;
}
示例14: flushPause
function flushPause($pause = 0)
{
echo ob_get_clean();
@ob_flush();
flush();
usleep($pause * 1000000);
}
示例15: send
public function send()
{
$nk = Nosql::NK_ASYNC_EMAIL_QUEUE;
$beginTime = time();
do {
do {
$rawMsg = Nosql::lPop($nk);
if ($rawMsg === false || !isset($rawMsg[0])) {
break;
}
$data = json_decode($rawMsg, true);
$ret = SendMail::sendmail($data['toList'], $data['title'], $data['desc']);
if ($ret === false) {
if (isset($data['retry'])) {
continue;
// drop it
} else {
$data['retry'] = 1;
Nosql::lPush($nk, json_encode($data));
}
}
} while (true);
if (time() - $beginTime > 30) {
// 30秒脚本重新执行一次
break;
}
usleep(200000);
} while (true);
}