Click here to get back home

returning partial executions to the user

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
returning partial executions to the user lazy 06-22-2008
Posted by lazy on June 22, 2008, 11:02 am
Please log in for more thread options
Hi,
I want to write a script such that it executes 2 mysql queries on the
server. But before executing the second query, I would like to return
the results of the first query to the user and then do my second query
or second query can go on asynchronously

Something like

<?php
$q1=...
mysql_query($q1)
//return the result to user before rest of the script executes

$q2 = ..

?>

Is it possible anyway to flush the output of 1st query to the user
before doing the 2nd query. Can it be done without threading?

ps: Are there any good docs on php threading. Googling didnt help much

thx

Posted by Jerry Stuckle on June 22, 2008, 11:06 am
Please log in for more thread options
lazy wrote:
> Hi,
> I want to write a script such that it executes 2 mysql queries on the
> server. But before executing the second query, I would like to return
> the results of the first query to the user and then do my second query
> or second query can go on asynchronously
>
> Something like
>
> <?php
> $q1=...
> mysql_query($q1)
> //return the result to user before rest of the script executes
>
> $q2 = ..
>
> ?>
>
> Is it possible anyway to flush the output of 1st query to the user
> before doing the 2nd query. Can it be done without threading?
>
> ps: Are there any good docs on php threading. Googling didnt help much
>
> thx
>

You can't reliably write output to the browser. You can flush the PHP
buffer, but the web server also has a buffer - and it won't send data
until its buffer is filled. Also, the browser won't necessarily show
incomplete data - again, depending on the browser.

You don't find much on PHP threading because you don't typically do
multithreading in PHP. Most PHP scripts (especially web based) are
meant to be short and sweet - get in, do the job and get out.

If you really require such a function, you may have to look at other
languages - a java applet can easily do what you want, for instance.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================


Posted by Iván Sánchez on June 22, 2008, 11:59 am
Please log in for more thread options
Jerry Stuckle wrote:

> lazy wrote:
>> I want to write a script such that it executes 2 mysql queries on the
>> server. But before executing the second query, I would like to return
>> the results of the first query to the user and then do my second query
>> or second query can go on asynchronously

If you totally *need* to do so, do it client-side. Make the browser request
the first set of data, and when loaded, make it request the second.
Javascript and some AJAX techniques will help.


> You don't find much on PHP threading because you don't typically do
> multithreading in PHP. Most PHP scripts (especially web based) are
> meant to be short and sweet - get in, do the job and get out.

Also, multithreading in web servers' processes doesn't usually go very
well - output race conditions and messing with the webserver's threading
model are a recipe for disaster. I speak from experience here. Please don't
try to do threading using the PHP apache module if you don't want to get a
headache.

OTOH, threading goes well with local scripting (when the PHP script is not
tied to a webserver). In this case, the PHP POSIX API works like any other
language - learn to do multithreading in C, you already know how to do
multithreading in PHP.


Cheers,
--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Proudly running Debian Linux with 2.6.24-1-amd64 kernel, KDE 3.5.9, and PHP
5.2.6-1 generating this signature.
Uptime: 17:53:17 up 14 days, 1:41, 4 users, load average: 0.41, 0.62,
0.58


Posted by lazy on June 22, 2008, 12:52 pm
Please log in for more thread options
On Jun 22, 8:59=A0am, Iv=E1n S=E1nchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.org> wrote:
> Jerry Stuckle wrote:
> > lazy wrote:
> >> I want to write a =A0script such that it executes 2 mysql queries on t=
he
> >> server. But before executing the second query, I would like to return
> >> the results of the first query to the user and then do my second query
> >> or second query can go on asynchronously
>
> If you totally *need* to do so, do it client-side. Make the browser reque=
st
> the first set of data, and when loaded, make it request the second.
> Javascript and some AJAX techniques will help.


Yeah, I thought of that, but the input data is same for both the
queries and it would be inefficient
request to send the same data again. The first query is the one the
user is waiting for and second query is more for internal
bookkeeping(which the user is not waiting for) but expensive query.



> > You don't find much on PHP threading because you don't typically do
> > multithreading in PHP. =A0Most PHP scripts (especially web based) are
> > meant to be short and sweet - get in, do the job and get out.
>
> Also, multithreading in web servers' processes doesn't usually go very
> well - output race conditions and messing with the webserver's threading
> model are a recipe for disaster. I speak from experience here. Please don=
't
> try to do threading using the PHP apache module if you don't want to get =
a
> headache.
>
> OTOH, threading goes well with local scripting (when the PHP script is no=
t
> tied to a webserver). In this case, the PHP POSIX API works like any othe=
r
> language - learn to do multithreading in C, you already know how to do
> multithreading in PHP.
>
> Cheers,
> --
> ----------------------------------
> Iv=E1n S=E1nchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-
>
> Proudly running Debian Linux with 2.6.24-1-amd64 kernel, KDE 3.5.9, and P=
HP
> 5.2.6-1 generating this signature.
> Uptime: 17:53:17 up 14 days, =A01:41, =A04 users, =A0load average: 0.41, =
0.62,
> 0.58


Posted by Iván Sánchez on June 22, 2008, 1:05 pm
Please log in for more thread options
lazy wrote:

> Yeah, I thought of that, but the input data is same for both the
> queries and it would be inefficient request to send the same data again.

You're doing prepature optimization. Don't. The amount of data needed for
the second query will likely be negligible.

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

503 Sig Not Found The Signature could not be accessed. Please try again
later.

Similar ThreadsPosted
Returning a URL from a user search October 9, 2006, 9:11 am
Returning NULL vs. returning FALSE November 18, 2005, 8:52 am
Partial Sort ? June 20, 2007, 3:18 am
javascript/php partial update August 16, 2004, 9:52 pm
Sending Partial Pages? August 31, 2005, 8:26 pm
partial ot ... stock market feed December 12, 2004, 7:25 pm
partial page loading problem October 12, 2006, 10:32 am
curl_exec() returns partial response with CURLOPT_RETURNTRANSFERon? February 2, 2005, 6:16 pm
Partial macro expansion of variable and function name. May 5, 2006, 12:21 pm
PHP script that displays another page partial content January 13, 2007, 10:45 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap