473,322 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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\php -f test.php", 'r');
stream_set_blocking($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 12376
"Tanel" <ri****@hot.ee> wrote in message
news:99**************************@posting.google.c om...
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\php -f test.php", 'r');
stream_set_blocking($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.google. 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\php -f test.php", 'r');
stream_set_blocking($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($process))" instead of "while(true)"
Jul 17 '05 #3
mc*******@earthlink.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($process))" 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\php -f test.php", 'r');
stream_set_blocking($process, false);
print "\n 1st fgets:".fgets($process);
sleep(3);
print "\n 2nd fgets:".fgets($process);
pclose($process);
Jul 17 '05 #4
"CountScubula" <me@scantek.hotmail.com> wrote in message news:<6HXOb.13433
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.ee> wrote in message
news:<99**************************@posting.google. com>...
"CountScubula" <me@scantek.hotmail.com> wrote in message news:<6HXOb.13433
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.google. com>...
mc*******@earthlink.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($process))" 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\php -f test.php", 'r');
stream_set_blocking($process, false);
do {
$line = fgets($process);
do_something_with_line($line);
do_something_else();
} while(!feof($process));
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\php -f test.php", 'r');
stream_set_blocking($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
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...
5
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...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
8
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...
14
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
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...
2
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...
0
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...
399
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.