doudula1974 2013-12-17 23:37
浏览 31
已采纳

用pthreads连续更新数组

Im trying to make a pthreads class that continuously updates a variable that I'll be using elsewhere in the script. The pthreads class should pull the contents of a URL, assign the value to the variable, sleep and repeat.

$pair = array();

class Pair extends Thread {
    public function __construct($url){
         $this->url = $url;
    }

    public function run() {
        global $pair;
        while (true){
        $pair = json_decode("https://btc-e.com/api/2/".$this->url."/ticker", true);
        sleep(5);
        }
    }
}

$BTC_USD = new Pair("btc_usd");

while (true){
    print_r($pair);
    sleep(5);
}

$pair needs to be continuously updated and printed to the screen

  • 写回答

1条回答 默认 最新

  • douman6679 2013-12-18 12:01
    关注
    <?php
    define ('SECOND', 1000000);
    
    class Repeater extends Thread {
        public $url;
        public $data;
    
        public function __construct($url) {
            $this->url = $url;
        }
    
        public function run() {
            do {
                /* synchronize the object */
                $this->synchronized(function(){
                    /* fetch data into object scope */
                    $this->data = file_get_contents($this->url);
    
                    /* wait or quit */
                    if (!$this->stop)
                        $this->wait();
                });
            } while (!$this->stop);
        }
    
        public function stop() {
            /* synchronize the object */
            $this->synchronized(function(){
                /* set stop */
                $this->stop = true;
    
                /* notify the object (could/should be waiting) */
                $this->notify();
            });
        }
    
        public function getData() {
            /* fetch data / wait for it to arrive */
            return $this->synchronized(function(){
                return $this->data;
            });
        }
    }
    
    /* to provide a limit for the example */
    $iterations = 5;
    
    /* the url etc is not important */
    $repeat = new Repeater("http://www.google.com");
    $repeat->start();
    
    /* this will only return when there is data */
    while (($data = $repeat->getData())) {
        var_dump(strlen($data));
    
        $repeat->synchronized(function($repeat){
            /* notify allows the thread to make he next request */
            $repeat->notify();
            /* waiting here for three seconds */
            $repeat->wait(3 * SECOND);
        }, $repeat);
    
        if (!--$iterations) {
            /* because examples should be sensible */
            $repeat->stop();
            break;
        }   
    }
    ?>
    

    It does not seem like a good use of resources to have a request per thread, you probably don't really want to do that in the real world. sleep is not suitable for use in multi-threading, it does not leave threads in a receptive state.

    globals have no effect in the context of threading, they work as they usually do with regard to scope, but it is not inclusive of the scope of a thread.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突