Adding Multi-Threading to PHP (Purely New Idea Try it)

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

ASDen
Forum Commoner
Posts: 55
Joined: Fri Aug 24, 2007 10:27 am

Post by ASDen »

To The Phoenix :-
PHP does not support multi-threading, period
This is a purely new note :P . please refer to my answer to astions & scottayy
You'd be just as well off with forking processes manually
Another repeated comment - you didn't read - refer to an earlier response to d11wtq
and wouldn't have issues with IE
what issues do the class have with IE it's just a SFF joke no more
I really don't think you understand the difference between threads and processes.
Thanks .... This what's really meant by constructive critique (note:this issue was also answered before)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

ASDen wrote:Well....... MONEY is talking here as while enabling JS is costless finding a server with fsockopen() enabled is a costy thing not free (in most cases) .
Well in that case my Swift Mailer project is doomed :(

In reality, it's a small proprtion of hosts that don't support fsockopen() because there's nothing dangerous about having it enabled.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Here's a good example of where Threading differs from process control.

Code: Select all

public class NumberThing {
  
  private int x = 0;

  protected static NumberThing instance = null;

  protected NumberThing() {
  }

  public static NumberThing getInstance() {
    if (instance == null) {
      instance = new NumberThing();
    }
    return instance;
  }

  public void incX() {
    x++;
  }

  public int getX() {
    return x;
  }
}

Code: Select all

public class NumberReader implements Runnable {

  public void run() {
    while (true) {
      try {
        System.out.println(NumberThing.getInstance().getX());
        Thread.sleep(1000);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

}

Code: Select all

public class NumberWriter implements Runnable {

  public void run() {
    while (true) {
      try {
        NumberThing.getInstance().incX();
        Thread.sleep(1000);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

}

Code: Select all

public class Test {

  public static void main(String[] args) {
    Thread t1 = new Thread(new NumberReader());
    Thread t2 = new Thread(new NumberWriter());
    t1.start();
    t2.start();
  }

}
Two threads, same memory:

Code: Select all

d11wtq@pc-cac:~/threads2$ java Test
0
1
2
3
4
5
6
7
8
9
You'd have to do a lot of jiggery pokery to do that with two processes.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

ASDen, you seem to be an extremely stubborn person.

I have read this entire post, and you have yet to show any proof that what you are doing is, in fact, multi-threading. PHP does not support it. It just doesn't. Are you sure that you even know what multi-threading is, because each time you try to explain it... You simply don't. You just state that your code accomplishes it.

Look at a language that natively supports multi-threading and how it works.

Hell, I am fairly sure that what you are doing doesn't even run the scripts simultaneously. When you use JavaScript to accomplish this, you are making multiple requests, leaving the rest of the process to the client's internet connection, not the server. I'm not sure how much experience you have with dial-up connections (as many people are so spoiled with their "blazing fast" internet speeds :P), but requests are not made instantly, and neither are the responses from the server. Though they may *seem* instant on broadband, they are not.

I'm not convinced.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Reliance on JS is a joke. A site that tells you that you have to have JS enabled in order to use it is asking that no one use it. It is an enhancement feature and should never be used as a requirement. Period.

Multiple requests does not equal multiple threads. Period.

I believe somewhere in this thread you mentioned using an Apache 2.2 web server with MPM. In fact, the PHP manual posts specific red letter warnings about this topic.

It is my opinion that you are attempting to mask multiple requests with the term multiple threads using a technology that not every browser supports under the assumption that if you tell the user to enable javascript that they will. Many posters have made several good points about the native ability for PHP to use sockets. Requiring JS to make multiple requests because 'ThinWire does it' or because 'AJAX is here' in not really an acceptable argument.
ASDen
Forum Commoner
Posts: 55
Joined: Fri Aug 24, 2007 10:27 am

Post by ASDen »

I put a second Example : a Tiny Spider :P (Web Crawler)
you can find it in : http://sourceforge.net/project/showfile ... _id=534860
please review it (should i open another thread or this is sufficient)
it doesn't support folder mapping now maybe in the near future (or your contributions :P )
To d11wtq :-
there's nothing dangerous about having it enabled.
first providing alternatives to standard Mail() - and other bandwidth bleeding things - is a Reason to prohibit it (it's enough that it's not enabled in 110mb)
You'd have to do a lot of jiggery pokery to do that with two processes.
This is not true.... You can do the same with the class in quietly the same Syntax .
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

ASDen wrote:
You'd have to do a lot of jiggery pokery to do that with two processes.
This is not true.... You can do the same with the class in quietly the same Syntax .
Ok then, post the PHP code that does what d11's example does. Not code from your class, but the code PHP code as translated from d11's Java code.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Your code is still practically unreadable and the comments/documentation need some work, for instance this method

Code: Select all

/*
 	Here is the real value setting put yet some simple string Mainpulation
  */
  function  Gset($name,$value)
  {
	$data=file_get_contents($this->UniqueName);
	$wh = fopen($data, 'wb');
	$q=strpos($data,$name);
	if ($q!=false){
	$z=strpos($data,"|",$q);	
	$c=substr($data,$q+strlen($name)+1,$z-($q+strlen($name)+1));	
	$Fdata=str_replace($c,$value,$data);}
	else{$Fdata=$data.$name."=".$value."|";}
	$wx=fopen($this->UniqueName,"w+");
	fwrite($wx,$Fdata);
	fclose($wx);
  }
What does Gset mean? The name of that method means nothing to me, your comment above the method means nothing to me, and the code inside the method is an unreadable mess.

Also, you have two classes, Threader and ThreadComHelper, which both share identical functions. Why not extend a base class?

Your documentation leads me nowhere. Even with the examples I would have no idea how to implement or use this class.
ASDen
Forum Commoner
Posts: 55
Joined: Fri Aug 24, 2007 10:27 am

Post by ASDen »

(Rest of the post)
There are other factors you didn't consider :-
1.The Cpu load :- using sockets will result in a high server cpu load (especially for high client number) compared to using this class as the load is shared between the Server and the Client
2.Connectivity to user :- This is shown more in my new example when using the class the threads is connected Directley to the user through a Js function (ChangeBag)
3. Memory Load :-using sockets from the server to it self will make to pay for all the memory cost (it opens the page handles both way connections)
while in the class the server opens the page and takes the responding part of the connection
To superdezign :-
PHP does not support it. It just doesn't
Another repeated notice it's the second repetition so i say it again I'm encapsulating a multi-threading like behavior with as little effort as possible
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

ASDen wrote:To superdezign :-
PHP does not support it. It just doesn't
Another repeated notice it's the second repetition so i say it again I'm encapsulating a multi-threading like behavior with as little effort as possible
Multi-threading like, or multi-process, is not multi-threading. you keep getting that response from people because you keep ignoring it. PHP does not support multi-threading. You have not yet shown that it does. All you have yet to do is show how you can implement new server processes.
ASDen
Forum Commoner
Posts: 55
Joined: Fri Aug 24, 2007 10:27 am

Post by ASDen »

When you use JavaScript to accomplish this, you are making multiple requests, leaving the rest of the process to the client's internet connection, not the server
Well.. this is a point in it's favor not against it and if you aren't sure of their simultaneously you can use it and read more about the Ajax Concept
but requests are not made instantly
considering their size they do.... And you forget that the important thing is running simultaneously on the server not sending simultaneously
To Everah :-
It is an enhancement feature and should never be used as a requirement
If you considered threading in the first place it's also an enhancement to performance rather than the sequential solution
so if clients are so strict about Js - and MOST aren't - you can use a sequential one as an alternate solution so is that case over ?
ASDen
Forum Commoner
Posts: 55
Joined: Fri Aug 24, 2007 10:27 am

Post by ASDen »

(rest of reply)
In fact, the PHP manual posts specific red letter warnings about this topic.
on understanding my class well you wouldn't say this . where in my class i ruined the follow
When you make the underlying framework more complex by not having completely separate execution threads, completely separate memory segments and a strong sandbox for each request to play in, feet of clay are introduced into PHP's system
please read how i implemented and offered Multi-Threading abilities in CODE
that not every browser supports
You'd better read more about Ajax before stating that not any browser supports it
Requiring JS to make multiple requests because 'ThinWire does it' or because 'AJAX is here' in not really an acceptable argument.
considering gains i stated and alternatives i see that there is no reason to neglect abilities Ajax offers
but the code PHP code as translated from d11's Java code.
too much of expected.... you want the code only just give me time
ASDen
Forum Commoner
Posts: 55
Joined: Fri Aug 24, 2007 10:27 am

Post by ASDen »

To Begby :-
What does Gset mean? The name of that method means nothing to me, your comment above the method means nothing to me, and the code
Gset (General Set) but what is the importance of the name i think the most important thing is the comment describing the job why do you think it means nothing ? it just states that this method is used to set values in the inter-thread communication
you see in communicating between threads i use a file with info, is stored in a form like simple XML (or much like of JSON) like this
"|Read=ON|Fire=cold|Listen=kindoff|"
for setting values or creating new ones use CSet which is enclosing GSet but locking read before calling it (to provide Sync.)
Why not extend a base class?
Thanks For this comment :idea: ... I'll really consider it
Even with the examples I would have no idea how to implement or use this class.
Why is that ?.... i think the example was very simple to clear the idea only small lines of code
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

ASDen wrote:To Everah :-
It is an enhancement feature and should never be used as a requirement
If you considered threading in the first place it's also an enhancement to performance rather than the sequential solution
so if clients are so strict about Js - and MOST aren't - you can use a sequential one as an alternate solution so is that case over ?
No, the case isn't over since you made absolutely no sense with what you are saying here.
ASDen wrote:(rest of reply)
In fact, the PHP manual posts specific red letter warnings about this topic.
on understanding my class well you wouldn't say this . where in my class i ruined the follow
When you make the underlying framework more complex by not having completely separate execution threads, completely separate memory segments and a strong sandbox for each request to play in, feet of clay are introduced into PHP's system
please read how i implemented and offered Multi-Threading abilities in CODE
So you are saying your code has completely separate execution threads, completely separate memory segments and a strong sandbox for each request to play in. Where in your code is this happening?
ASDen wrote:
that not every browser supports
You'd better read more about Ajax before stating that not any browser supports it
Thanks teacher, I'll get right on that. Are you seriously trying to imply that every browser supports Javascript at the present time? Please. And in that same breath, any browser that supports the disabling of Javascript will in effect destroy your applications behavior, so until you can force a browser to keep javscript enabled your code relies on a technology that is not necessarily available to your application.
ASDen wrote:
Requiring JS to make multiple requests because 'ThinWire does it' or because 'AJAX is here' in not really an acceptable argument.
considering gains i stated and alternatives i see that there is no reason to neglect abilities Ajax offers
No one is neglecting the abilities of Ajax. I am merely saying that relying on it is a bad assumption you are making.
ASDen wrote:
but the code PHP code as translated from d11's Java code.
too much of expected.... you want the code only just give me time
Take all the time you need.
ASDen
Forum Commoner
Posts: 55
Joined: Fri Aug 24, 2007 10:27 am

Post by ASDen »

Counter.php

Code: Select all

<?php
require("ThreadComHelper.php");
$e=new ThreadComHelper("Jsim",0);
for($i=1;$i<=10;$i++)
{
	$e->CSet("Counter",$i);
	sleep(1);
}
?>
Echoer.php

Code: Select all

<?php
require("ThreadComHelper.php");
$s=new ThreadComHelper("Jsim",0);
for($f=1;$f<=10;$f++)
{
	$count=$s->CGet("Counter");
	echo "document.getElementById(\"M\").innerHTML=\"$count\";";
	flush();	
	sleep(1);
}
?>
Jsim.php

Code: Select all

<form name="Trail" action="POST">
<input type="text" name="T1">
</form>
<div id="M"></div>
<?php
require("Threader.php");
$e=new Thread("Counter.php","","Jsim",0);
$s=new Thread("Echoer.php","","Jsim",1);
$e->Go();
$s->Go();
?>
[/quote]
Post Reply