rain16881 发表于 2011-10-14 11:32:22

请问。。CI的timer怎样用的。。求教程。求思路

大家好。真不好意思。。用CI真的不长时间。。现在还在用1.7.2。。

我php是自学的。。还不是太懂。。只懂做一般的小网站。。小功能。。调用一下API。。

小弟我在这很想求CI的timer的教程。。当然我自己也会看相关的资料。。本来不是饭来张口的人。。

不过小弟我真的找不到资料。。自己看源代码。。也有很多不明的地方。。CI的强大。。

很多机制同java相同。。不过写法真的差天共地。。教程不用写得太明白的。。给我个思路就好了。。谢谢

jeongee 发表于 2011-10-14 11:35:34

php里没有timer的东西,要么用循环+sleep来模拟,要么利用linux的crontab(PHP需要在cgi模式下)

rain16881 发表于 2011-10-14 11:45:11

jeongee 发表于 2011-10-14 11:35 static/image/common/back.gif
php里没有timer的东西,要么用循环+sleep来模拟,要么利用linux的crontab(PHP需要在cgi模式下) ...

/*
* ------------------------------------------------------
*Start the timer... tick tock tick tock...
* ------------------------------------------------------
*/

$BM =& load_class('Benchmark');
$BM->mark('total_execution_time_start');
$BM->mark('loading_time_base_classes_start');
这个CI入边的不是定时器吗!?

rain16881 发表于 2011-10-14 11:49:12

jeongee 发表于 2011-10-14 11:35 static/image/common/back.gif
php里没有timer的东西,要么用循环+sleep来模拟,要么利用linux的crontab(PHP需要在cgi模式下) ...

我看一下在CI 中的controllor中 循环+sleep来模拟 能不能做吧。。好的。。谢谢

baiyuxiong 发表于 2011-10-14 11:56:23

rain16881 发表于 2011-10-14 11:45 static/image/common/back.gif
这个CI入边的不是定时器吗!?

这个不是定时器

这个是计时器,记录执行一个操作用了多长时间,一般用来性能调试的。

rain16881 发表于 2011-10-14 12:02:07

baiyuxiong 发表于 2011-10-14 11:56 static/image/common/back.gif
这个不是定时器

这个是计时器,记录执行一个操作用了多长时间,一般用来性能调试的。 ...

好的。。谢谢。。如果这个不是定时器。。我就不在向这个方向去找资料了。。我再找找相关php的定时器的资料吧。。

baiyuxiong 发表于 2011-10-14 13:31:57

rain16881 发表于 2011-10-14 12:02 static/image/common/back.gif
好的。。谢谢。。如果这个不是定时器。。我就不在向这个方向去找资料了。。我再找找相关php的定时器的资 ...

你要定时器做什么?
什么需求?

rain16881 发表于 2011-10-16 21:26:13

baiyuxiong 发表于 2011-10-14 13:31 static/image/common/back.gif
你要定时器做什么?
什么需求?

就最简单的。。可以在后台运行。。关了php页也可以运行。。有开关。。
<?php

interface ITimerHandler
{

    public function handleTimer();
}
declare(ticks=1);
/**
* Timer helper. Will call your handler every so milliseconds.
*
* PHP Version 5
*
* @category   Ding
* @package    Helpers
* @subpackage Timer
* @author   Marcelo Gornstein <marcelog@gmail.com>
* @license    http://marcelog.github.com/ Apache License 2.0
* @version    SVN: $Id$
* @link       http://marcelog.github.com/
*
* Copyright 2011 Marcelo Gornstein <marcelog@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
namespace Ding\Helpers\Timer;

/**
* Timer helper. Will call your handler every so milliseconds.
*
* PHP Version 5
*
* @category   Ding
* @package    Helpers
* @subpackage Timer
* @author   Marcelo Gornstein <marcelog@gmail.com>
* @license    http://marcelog.github.com/ Apache License 2.0
* @link       http://marcelog.github.com/
*/
class TimerHelper
{
    /**
   * Timer handler.
   * @var ITimerHandler
   */
    private $_handler;

    /**
   * Timer interval, in milliseconds, doh.
   * @var integer
   */
    private $_milliseconds;

    /**
   * Time for last expiration (or start) of this timer.
   * @var float
   */
    private $_start;

    /**
   * Used internally for tick to discrimine when the timer should be running.
   * @var boolean
   */
    private $_running;

    /**
   * Sets the handler for this timer.
   *
   * @param ITimerHandler $handler Handler to call when timer expires.
   *
   * @return void
   */
    public function setHandler(ITimerHandler $handler)
    {
      $this->_handler = $handler;
    }

    /**
   * Configure the timer, set the amount of milliseconds.
   *
   * @param integer $milliseconds Timer interval.
   *
   * @return void
   */
    public function setMilliseconds($milliseconds)
    {
      $this->_milliseconds = $milliseconds;
    }

    /**
   * Evaluates if the timer has expired and if so, call the handler.
   *
       * @return void
   */
    public function tick()
    {
      if (!$this->_running) {
            return;
      }
      $end = $this->getMicrotime();
      $total = ($end - $this->_start) * 1000;
      if ($total >= $this->_milliseconds) {
            $this->_handler->handleTimer();
            $this->_start = $this->getMicrotime();
      }
    }

    /**
   * From php examples. Returns time including millseconds.
   *
   * @todo duplicated code, where can this go?
   * @return float
   */
    protected function getMicrotime()
    {
      list($usec, $sec) = explode(" ", microtime());
      return ((float)$usec + (float)$sec);
    }

    /**
   * Starts the timer.
   *
   * @return void
   */
    public function start()
    {
      $this->_start = $this->getMicrotime();
      $this->_running = true;
      register_tick_function(array($this, 'tick'));
    }

    /**
   * Stops the timer.
   *
   * @return void
   */
    public function stop()
    {
      $this->_running = false;
      unregister_tick_function(array($this, 'tick'));
    }

    /**
   * Constructor.
   *
   * @return void
   */
    public function __construct()
    {
      $this->_running = false;
    }
}
<?php
declare(ticks=1);
/**
* Syslog example
*
* PHP Version 5
*
* @category   Ding
* @package    Examples
* @subpackage TimerHelper
* @author   Marcelo Gornstein <marcelog@gmail.com>
* @license    http://marcelog.github.com/ Apache License 2.0
* @version    SVN: $Id$
* @link       http://marcelog.github.com/
*
* Copyright 2011 Marcelo Gornstein <marcelog@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
ini_set(
    'include_path',
    implode(
      PATH_SEPARATOR,
      array(
            implode(DIRECTORY_SEPARATOR, array('..', '..', '..', 'src', 'mg')),
            ini_get('include_path'),
      )
    )
);

require_once 'Ding/Autoloader/Autoloader.php'; // Include ding autoloader.
\Ding\Autoloader\Autoloader::register(); // Call autoloader register for ding autoloader.

use Ding\Container\Impl\ContainerImpl;
use Ding\Helpers\Timer\ITimerHandler;

class MyTimerHandler1 implements ITimerHandler
{
    public function handleTimer()
    {
      echo "hello there every five seconds\n";
    }
}

class MyTimerHandler2 implements ITimerHandler
{
    public function handleTimer()
    {
      echo "hello there every second\n";
    }
}

$properties = array(
    'ding' => array(
      'log4php.properties' => './log4php.properties',
      'factory' => array(
            'bdef' => array('xml' => array('filename' => 'beans.xml')),
            'properties' => array(
                'interval1' => 5000,
                'interval2' => 1000
            )
      ),
      'cache' => array(
            'proxy' => array('impl' => 'dummy', 'directory' => '/tmp/Ding/proxy'),
            'bdef' => array('impl' => 'dummy', 'directory' => '/tmp/Ding/bdef'),
            'beans' => array('impl' => 'dummy')
      )
    )
);
$a = ContainerImpl::getInstance($properties);
$timer1 = $a->getBean('Timer1');
$timer2 = $a->getBean('Timer2');
$timer1->start();
$timer2->start();
while(true){
    usleep(1);
}


baiyuxiong 发表于 2011-10-16 22:15:11

这个你了解下PHP的后台进程的实现吧

pxc2471418 发表于 2011-10-17 16:58:36

可以用电脑的事务日志来处理呀
建立bat文件
在事务日志里面设置调用
如:
E:\xampp\php\php.exe -q E:\www\sendmail\index.php
上面的bat代码你应该能看懂
以前用过
页: [1] 2
查看完整版本: 请问。。CI的timer怎样用的。。求教程。求思路