用户
 找回密码
 入住 CI 中国社区
搜索
查看: 6414|回复: 12
收起左侧

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

[复制链接]
发表于 2011-10-14 11:32:22 | 显示全部楼层 |阅读模式
大家好。真不好意思。。用CI真的不长时间。。现在还在用1.7.2。。

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

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

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

很多机制同java相同。。不过写法真的差天共地。。教程不用写得太明白的。。给我个思路就好了。。谢谢
发表于 2011-10-14 11:35:34 | 显示全部楼层
php里没有timer的东西,要么用循环+sleep来模拟,要么利用linux的crontab(PHP需要在cgi模式下)
 楼主| 发表于 2011-10-14 11:45:11 | 显示全部楼层
jeongee 发表于 2011-10-14 11:35
php里没有timer的东西,要么用循环+sleep来模拟,要么利用linux的crontab(PHP需要在cgi模式下) ...
PHP复制代码
/*
 * ------------------------------------------------------
 *  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入边的不是定时器吗!?
 楼主| 发表于 2011-10-14 11:49:12 | 显示全部楼层
jeongee 发表于 2011-10-14 11:35
php里没有timer的东西,要么用循环+sleep来模拟,要么利用linux的crontab(PHP需要在cgi模式下) ...

我看一下在CI 中的controllor中 循环+sleep来模拟 能不能做吧。。好的。。谢谢
发表于 2011-10-14 11:56:23 | 显示全部楼层
rain16881 发表于 2011-10-14 11:45
这个CI入边的不是定时器吗!?

这个不是定时器

这个是计时器,记录执行一个操作用了多长时间,一般用来性能调试的。
 楼主| 发表于 2011-10-14 12:02:07 | 显示全部楼层
baiyuxiong 发表于 2011-10-14 11:56
这个不是定时器

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

好的。。谢谢。。如果这个不是定时器。。我就不在向这个方向去找资料了。。我再找找相关php的定时器的资料吧。。
发表于 2011-10-14 13:31:57 | 显示全部楼层
rain16881 发表于 2011-10-14 12:02
好的。。谢谢。。如果这个不是定时器。。我就不在向这个方向去找资料了。。我再找找相关php的定时器的资 ...

你要定时器做什么?
什么需求?
 楼主| 发表于 2011-10-16 21:26:13 | 显示全部楼层
baiyuxiong 发表于 2011-10-14 13:31
你要定时器做什么?
什么需求?

就最简单的。。可以在后台运行。。关了php页也可以运行。。有开关。。
PHP复制代码
<?php
 
interface ITimerHandler
{
 
    public function handleTimer();
}
复制代码
PHP复制代码
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复制代码
<?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);
}
 
复制代码


发表于 2011-10-16 22:15:11 | 显示全部楼层
这个你了解下PHP的后台进程的实现吧
发表于 2011-10-17 16:58:36 | 显示全部楼层
可以用电脑的事务日志来处理呀
建立bat文件
在事务日志里面设置调用
如:
E:\xampp\php\php.exe -q E:\www\sendmail\index.php
上面的bat代码你应该能看懂
以前用过

本版积分规则