473,549 Members | 2,642 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to do a non-blocking fgets?

Hello,

I need to read a result of the first script (that takes some time to
run) from the second script so that the first script doesn't block the
second. Here is a short example. do_smth_else() should be called
during test.php's sleep, instead the first fgets blocks the processing
of the parent script and do_smth_else is called only once. How can I
do a non-blocking fgets()? I use php 4.3.4 on windows xp

$process = popen("c:\php\p hp -f test.php", 'r');
stream_set_bloc king($process, false);
do {
$line = fgets($process) ;
print $line;
do_smth_else();
if ($line === false) break;
} while(true);
pclose($process );

contents of the test.php:
<?php echo 'Hello';sleep(2 ); echo ' World!'; ?>
Jul 17 '05 #1
6 12392
"Tanel" <ri****@hot.e e> wrote in message
news:99******** *************** ***@posting.goo gle.com...
Hello,

I need to read a result of the first script (that takes some time to
run) from the second script so that the first script doesn't block the
second. Here is a short example. do_smth_else() should be called
during test.php's sleep, instead the first fgets blocks the processing
of the parent script and do_smth_else is called only once. How can I
do a non-blocking fgets()? I use php 4.3.4 on windows xp

$process = popen("c:\php\p hp -f test.php", 'r');
stream_set_bloc king($process, false);
do {
$line = fgets($process) ;
print $line;
do_smth_else();
if ($line === false) break;
} while(true);
pclose($process );

contents of the test.php:
<?php echo 'Hello';sleep(2 ); echo ' World!'; ?>


sounds like you are a VB programmer looking for doevents() or you want to
spawn a background thread.

To the best of my knowlendge there is no *simple* way of doing multithreads
*within* a php script.

my advice, rethink what you are trying to acomplish, and shell out of the
script and launch a new proccess in the background: `php -q script.php &`;
--
Mike Bradley
http://www.gzentools.com -- free online php tools
Jul 17 '05 #2
ri****@hot.ee (Tanel) wrote in message news:<99******* *************** ****@posting.go ogle.com>...
Hello,

I need to read a result of the first script (that takes some time to
run) from the second script so that the first script doesn't block the
second. Here is a short example. do_smth_else() should be called
during test.php's sleep, instead the first fgets blocks the processing
of the parent script and do_smth_else is called only once. How can I
do a non-blocking fgets()? I use php 4.3.4 on windows xp

$process = popen("c:\php\p hp -f test.php", 'r');
stream_set_bloc king($process, false);
do {
$line = fgets($process) ;
print $line;
do_smth_else();
if ($line === false) break;
} while(true);
pclose($process );

contents of the test.php:
<?php echo 'Hello';sleep(2 ); echo ' World!'; ?>


Are you sure it's blocking and not just looping once? I get
do_smth_else() being called once, but also no "Hello World" and
with fgets() returning "false" the first time, as expected. It
works for me without the "if ($line == false) break;" line and
with "while(!feof($p rocess))" instead of "while(true )"
Jul 17 '05 #3
mc*******@earth link.net (martin) wrote in message >
Are you sure it's blocking and not just looping once? I get
do_smth_else() being called once, but also no "Hello World" and
with fgets() returning "false" the first time, as expected. It
works for me without the "if ($line == false) break;" line and
with "while(!feof($p rocess))" instead of "while(true )"


Notice the three = in ($line === false). Here is maybe a better
example to show what I mean. The first fgets waits until the test.php
has finished, and second fgets has nothing. Desired behavior should be
1st fgets:Hello, 2nd fgets World!

$process = popen("c:\php\p hp -f test.php", 'r');
stream_set_bloc king($process, false);
print "\n 1st fgets:".fgets($ process);
sleep(3);
print "\n 2nd fgets:".fgets($ process);
pclose($process );
Jul 17 '05 #4
"CountScubu la" <me@scantek.hot mail.com> wrote in message news:<6HXOb.134 33
To the best of my knowlendge there is no *simple* way of doing multithreads
*within* a php script.

my advice, rethink what you are trying to acomplish, and shell out of the
script and launch a new proccess in the background: `php -q script.php &`;


I am trying to make a php-gtk utility which uses curl to get some web
content. So far the curl_exec blocks the whole script (until download
is finished) and gui freezes. I also need to start to download a
second page before the first is finished, so I have multiple streams I
need to check for arriving data. All that time the gui should be
responsive. It seams that its not an easy task in php..
Jul 17 '05 #5
"Tanel" <ri****@hot.e e> wrote in message
news:<99******* *************** ****@posting.go ogle.com>...
"CountScubu la" <me@scantek.hot mail.com> wrote in message news:<6HXOb.134 33
To the best of my knowlendge there is no *simple* way of doing multithreads *within* a php script.

my advice, rethink what you are trying to acomplish, and shell out of the script and launch a new proccess in the background: `php -q script.php
&`;
I am trying to make a php-gtk utility which uses curl to get some web
content. So far the curl_exec blocks the whole script (until download
is finished) and gui freezes. I also need to start to download a
second page before the first is finished, so I have multiple streams I
need to check for arriving data. All that time the gui should be
responsive. It seams that its not an easy task in php..


if this is a shell script with gui front, then it might be easier to launch
other proccesses instead of trying to multithread. launch your get php
script with
`php -q script.php $url &`;
or
`wget $url &`;

both will run in background while your gui is active.

--
Mike Bradley
http://www.gzentools.com -- free online php tools
Jul 17 '05 #6
ri****@hot.ee (Tanel) wrote in message news:<99******* *************** ****@posting.go ogle.com>...
mc*******@earth link.net (martin) wrote in message >
Are you sure it's blocking and not just looping once? I get
do_smth_else() being called once, but also no "Hello World" and
with fgets() returning "false" the first time, as expected. It
works for me without the "if ($line == false) break;" line and
with "while(!feof($p rocess))" instead of "while(true )"
Notice the three = in ($line === false).


Same difference. "false" is the empty string. fgets() returns
an empty string when reading from a non-blocking file-descriptor
which has nothing to read. "if ($line === false) break;" will
surely break out of your loop when $line comes from such a read,
as it surely must for at least the first read. Fgets() will not
wait for anything when reading from a non-blocking file-descriptor.

If, as you said, you want to "read a result of the first script
(that takes some time to run) from the second script so that the
first script doesn't block the second", and meanwhile be doing
something else, this will do that for you until the first script
is finished. (your example code modified):

$process = popen("c:\php\p hp -f test.php", 'r');
stream_set_bloc king($process, false);
do {
$line = fgets($process) ;
do_something_wi th_line($line);
do_something_el se();
} while(!feof($pr ocess));
pclose($process );

Here is maybe a better
example to show what I mean. The first fgets waits until the test.php
has finished, and second fgets has nothing. Desired behavior should be
1st fgets:Hello, 2nd fgets World!

$process = popen("c:\php\p hp -f test.php", 'r');
stream_set_bloc king($process, false);
print "\n 1st fgets:".fgets($ process);
sleep(3);
print "\n 2nd fgets:".fgets($ process);
pclose($process );

Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
4404
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few characters as possible will be matched. the regular expression module fails to perform non-greedy matches as described in the documentation: more than...
5
3730
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence their dtor is called immediately and not at the end of the function. to be able to use return objects (to avoid copying) i often assign them to a const...
25
7565
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
4471
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
8
3492
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I cannot load/unload/reload extensions into my large and slow-to-load application during development without restarting the process then the...
14
8362
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
11
3402
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the vtable pointer is made use of.. eg. class one {
2
6096
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my pointers, but I'm not sure. Please help. The code: void equate(matrix *A, matrix *B) { int i, j; assert(A.row_dim == B.col_dim && A.col_dim ==...
0
2323
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome jobs. When a non-secure page references a secure page with relative URL, the web server generates error until absolute URL with https prefix is...
399
12692
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or to python-3000@python.org In summary, this PEP proposes to allow non-ASCII letters as identifiers in Python. If the PEP is accepted, the...
0
7532
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7462
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7730
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7975
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7823
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5101
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3510
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3491
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.