如何使pthreads在PHP中工作? [英] How to get pthreads working in PHP?

查看:88
本文介绍了如何使pthreads在PHP中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用wampserver测试&在我的本地计算机上运行wordpress代码.为了运行pthread,我遵循了以下步骤:

I am using wampserver to test & run wordpress code in my local computer. In order to run pthread, I have followed the following steps:

1)我从 http://windows获得了pthread zip文件. php.net/downloads/pecl/releases/pthreads/0.44/ (我的机器上安装了php 5.3.13,并从上面的链接下载了php_pthreads-0.44-5.3-ts-vc9-x86.zip file.)

1) I got the pthread zip file from http://windows.php.net/downloads/pecl/releases/pthreads/0.44/ (My machine has php 5.3.13 and downloaded the php_pthreads-0.44-5.3-ts-vc9-x86.zip file from the above link).

2)提取了zip文件.将php_pthreads.dll移至C:\wamp\bin\php\php5.3.13\ext目录.

2) Extracted the zip file. Moved the php_pthreads.dll to the C:\wamp\bin\php\php5.3.13\ext directory.

3)将pthreadVC2.dll移至C:\wamp\bin\php\php5.3.13目录.

3) Moved pthreadVC2.dll to the C:\wamp\bin\php\php5.3.13 directory.

4)然后打开C:\wamp\bin\php\php5.3.13\php.ini,并在文件开头添加代码extension=php_pthreads.dll.

4) Then Opened C:\wamp\bin\php\php5.3.13\php.ini and added the code extension=php_pthreads.dll at the begining of the file.

但是当我尝试运行以下代码时:

But when I try to run the following code:

<?php
class My extends Thread {
    public function run() {
        printf("%s is Thread #%lu\n", __CLASS__, $this->getThreadId());
    }
}
$my = new My();
$my->start();
?>

它给了我以下错误:

Fatal error: Class 'Thread' not found in C:\wamp\www\wp-admin\includes\post.php on line 2

您能告诉我如何在计算机上安装pthread以便与php一起使用吗?我还必须安装其他软件吗?

Can you please tell me how to install pthreads in my computer to use with php? and do I have to install any other software?

推荐答案

我注意到wampserver在两个不同的位置都有php.ini.一个地方在/wamp/bin/php/php5 ...目录中,另一地方在/wamp/bin/apache/apache.../bin目录中(其中"..."代表版本号) .这两个文件必须相同,因为显然它们是在整个wampserver启动过程中在不同的时间加载的.

I've noticed that wampserver has php.ini in two separate places. One place is in the /wamp/bin/php/php5... directory, and the other place is in the /wamp/bin/apache/apache.../bin directory (where "..." represents version numbers). The two files need to be identical, because apparently both are loaded at different times by the overall wampserver boot-up procedure.

(请注意,我是最近才发现的,可能与使用wampserver做花哨的事情很落后" –也许其他所有人都已经很长时间都在处理这两个文件了.所以我不知道是否简单的事情可以解决您的问题;我来这里的目的是寻找我自己的信息,有关做一些多线程的事情.:)

(Note I only discovered this recently, and may be well "behind the curve" of doing fancy things with wampserver --maybe everyone else has been dealing with both files for a long time. So I don't know if this simple thing will fix your problem; I came here looking for info, myself, regarding doing some multi-threading stuff. :)

另一件事.根据此页面:www.php.net/manual/en/pthreads.requirements.php 为了使pthreads起作用,必须使用"--enable-zts"编译PHP.我找不到任何证据证明wampserver的PHP部分是通过这种方式编译的.

One other thing. According to this page: www.php.net/manual/en/pthreads.requirements.php PHP has to be compiled with "--enable-zts" in order for pthreads stuff to work. I have not been able to find any evidence that the PHP part of wampserver was compiled that way.

(几个月后) 决定自己并不需要立即做任何线程处理工作之后,我继续做其他事情,直到实际需要为止.我现在可以说,编译入WampServer的PHP版本确实支持"pthread"扩展,尽管首先需要进行一些设置工作.我看到的说明提到将几个.dll文件(在下载并解压缩后)放入某些位置,但这对我不起作用.将它们复制到\ Windows \ System32目录即可. (将它们放入\ apache ... \ bin目录也可以;其中还有一些其他php .dll文件.)

(months later) Having decided I didn't really immediately need to do any threading stuff, I went on to do other things, until the need actually arose. I now can say that the version of PHP compiled into WampServer does support the "pthread" extension, although some set-up work is needed, first. The instructions I saw mentioned putting a couple of .dll files (after a download and unZip) into certain places, but that didn't work for me. Copying them to the \Windows\System32 directory did work. (Putting them into the \apache...\bin directory also works; there are some other php .dll files in there.)

此后,就像您所做的一样,有必要定义一个扩展"Thread"类的类",以便在另一个线程中实际执行某项操作. Thread类中的"run()"函数是抽象的",需要在扩展类中作为实际函数实现".然后,"new"运算符可以创建"instance"(该指定类的对象)以供实际使用.这是我需要的课程:

After that, much like what you did, it is necessary to define a "class" that extends the "Thread" class, in order to actually do something in another thread. The "run()" function in the Thread class is "abstract", and needs to be "realized" as an actual function in the extended class. Then the "new" operator can create an "instance", an object of that specified class, for actual use. Here's the class that I needed:

//Purpose: Use another thread to run the code in another php file, after a delay
class xT extends Thread
{ var $fil, $tim;

  function savWhatWhen($f="", $t=0)
  { $this->fil = $f;                    //save What, file to process
    $this->tim = $t;                    //save When, delay before processing file
    return;
  }

  function run()
  { ini_set('max_execution_time', 600); //600 seconds = 10 minutes
    sleep($this->tim);                  //do a delay; beware of max-exec-time!
    include($this->fil);                //load file-to-process, and process it
    return;
} }

该"savWhatWhen()"函数是专门为基本Thread类的此扩展而创建的.这是使用该类的一些代码:

That "savWhatWhen()" function was created specifically for this extension of the basic Thread class. Here's some code for using that class:

  $TH = new xT();                                    //prepare a separate Thread
  $TH->savWhatWhen("d:/wamp/myscripts/test.php", 45);//file-name and delay time
  $TH->start();                                      //after delay, process file
  //the code that does this can terminate, while OTHER thread is doing a delay

对于任何复制此代码的人来说,您可能需要确保php.ini中的"open_basedir"设置允许访问指定的文件.

Note for anyone copying this code, you might need to make sure your "open_basedir" setting in the php.ini allows access to the specified file.

更多月后:由于有许多工作要做,因此我没有花很多时间使用我的pthread对象.我确实遇到了一个特殊性,这使我想知道我是否真的可以按照我希望的方式使用pthreads.这是我观察到的: 1.一个初始的php文件被AJAX调用,以执行某项操作. 2. Web服务器上的PHP处理器执行该操作. 3.应该将各种数据回显到浏览器. 4.最初的php文件要求创建另一个线程,然后终止. 5.浏览器尚未收到回显的数据! 6. Web服务器上的PHP处理器完成委派给第二个线程的工作. 7.当第二个线程终止时,现在浏览器将收到回显的数据!

More months later: With lots of things being worked on, I haven't put a lot of time into using my pthread object. I did encounter a peculiarity that makes me wonder about whether or not I can actually use pthreads the way I had hoped. Here is what I have observed: 1. An initial php file is called by AJAX, to do something. 2. The PHP processor on the Web Server does that thing. 3. Various data is supposed to be echoed to the browser. 4. The initial php file calls for the creation of another thread, and terminates. 5. The browser does not yet receive the echoed data! 6. The PHP processor on the Web Server does the work delegated to the second thread. 7. When the second thread terminates, NOW the browser receives the echoed data!

在撰写本文时,我想我错过了一些东西.也许我需要在第一个线程结束时执行一些强制的刷新"操作,以便浏览器可以接收回显的数据,并且用户可以在服务器上的PHP处理器也在执行操作的同时执行操作.

At this writing I'm thinking I missed something. Perhaps I need to do some forceful "flush" stuff when the first thread ends, so that the browser can receive the echoed data and the user can do things while the PHP processor on the server is also doing things.

这篇关于如何使pthreads在PHP中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆