PHP Multi-Threading!

Status
Not open for further replies.

deeTrix

Banned
Banned
157
2012
27
0
I was browsing the web the other day and saw something rather interesting, haven't had much time to test it out (Just a brief glance through). But thought it'd be quite interesting considering many people don't think about PHP as a language that could ultimately be brought to the desktop due to limitations in Multi-Threading.

Anyway! Here it is!
Code:
http://pthreads.org
*I do not own nor did i code the above extension. Just merely found it.

Could possibly be used in extension with WinBinder!

Happy exploring :)
deeTrix
 
1 comment
I was browsing the web the other day and saw something rather interesting, haven't had much time to test it out (Just a brief glance through). But thought it'd be quite interesting considering many people don't think about PHP as a language that could ultimately be brought to the desktop due to limitations in Multi-Threading.

Anyway! Here it is!
Code:
http://pthreads.org
*I do not own nor did i code the above extension. Just merely found it.

Could possibly be used in extension with WinBinder!

Happy exploring :)
deeTrix

I love the idea of WinBinder, many moons ago I was quite in to it ...

So here's the wb_wait example threaded, the original example highlighted exactly why you couldn't really use WinBinder for anything complex ... the problem is completely voided by pthreads, WinBinder is very compatible, just by chance :)

In the rewritten example, there's just one worker thread to do background work, which is exactly the sort of execution model you would/should use, the interface remains properly responsive, all the time ...

Here's a build of PHP5.4.11 (VC9 X86) with winbinder and pthreads and this example, launch wb.bat to run ...

Have fun :)

Code:
<?php

/*******************************************************************************

 WINBINDER - The native Windows binding for PHP for PHP

 Copyright © Hypervisual - see LICENSE.TXT for details
 Author: Rubem Pechansky (http://winbinder.org/contact.php)

 How to use the wb_wait() function

*******************************************************************************/

//------------------------------------------------------------ SYSTEM PARAMETERS

define("PATH_SCRIPT",    dirname(__FILE__) . "/");
define("PATH_DATA",        PATH_SCRIPT);
define("PATH_INC",        PATH_SCRIPT . "include/");
define("PATH_RES",        PATH_SCRIPT . "resources/");

//----------------------------------------------------------------- DEPENDENCIES

include PATH_INC . "winbinder.php";

//-------------------------------------------------------------------- CONSTANTS

define("APPNAME",         "pthreads demonstration");
define("ID_START",        202);
define("ID_CHAR",        203);
define("ID_INTERVAL",    204);
define("ID_STATUSBAR",    101);

//-------------------------------------------------------------- EXECUTABLE CODE

// Create main window and controls

define("WB_MAIN", wb_create_window(NULL, PopupWindow, APPNAME,
  WBC_CENTER, WBC_CENTER, 200, 200, 0, 0));

wb_create_control(WB_MAIN, StatusBar, "", 0,0,0,0,ID_STATUSBAR);
wb_create_control(WB_MAIN, PushButton, "Start", 10, 10, 80, 40, ID_START);
wb_create_control(WB_MAIN, Label, "Interval:", 120, 10, 165, 15);
wb_create_control(WB_MAIN, EditBox, "", 120, 30, 40, 18, ID_INTERVAL, WBC_NUMBER);
wb_create_control(WB_MAIN, Spinner, "", 162, 30, 20, 18, 0, WBC_GROUP);
wb_create_control(WB_MAIN, Label, "!", 10, 90, 165, 40, ID_CHAR, WBC_CENTER);

wb_set_value(wb_get_control(WB_MAIN, ID_INTERVAL), 20, 1, 5000);
wb_set_font(wb_get_control(WB_MAIN, ID_CHAR), wb_create_font("Arial", 24, BLACK, FTA_BOLD));

wb_set_handler(WB_MAIN, "process_main");

class WbWorker extends Worker {    
    public function run(){}
}

class WbWait extends Stackable {
    public function run(){
        $charcmd = wb_get_control(WB_MAIN, ID_CHAR);
        $interval = wb_get_value(wb_get_control(WB_MAIN, ID_INTERVAL));
        $statusbar = wb_get_control(WB_MAIN, ID_STATUSBAR);
        $start = wb_get_control(WB_MAIN, ID_START);
        
        wb_set_text($statusbar, "Looping.");

        wb_set_enabled($start, false);
        for($char = 33; $char < 127; $char++) {
            wb_set_text($charcmd, chr($char));

            wb_refresh($charcmd);
            
            usleep($interval*100);
        }
        wb_set_text($statusbar, "Done.");
        wb_set_enabled($start, true);
    }
}

function process_main($window, $id, $ctrl=0, $lparam=0, $lparam2=0)
{
    global $worker, $work;
    
    switch($id) {

        case ID_START: {
            $work = new WbWait();
            $worker->stack($work);
        } break;

        case ID_USEIT:

            break;

        case IDCLOSE:
            wb_destroy_window($window);
            break;
    }
}

$worker = new WbWorker();
$worker->start();
wb_main_loop();
//-------------------------------------------------------------------------- END

?>

PS. I did write pthreads ...
 
Last edited:
Status
Not open for further replies.
Back
Top