Thread Browser Keep-alive (19 answers)
Opened by bianca at 2010-05-28 19:58

bianca
 2010-05-30 17:44
#137777 #137777
User since
2009-09-13
7016 Artikel
BenutzerIn

user image
2010-05-30T13:03:55 pq
mache ein fork und führe im child das programm aus, im parent schickst du dem browser eine refresh-seite, mit der du alle 5 sekunden nachguckst, ob dein geforkter prozess schon fertig ist.

Gute Idee! Und da es ohnehin um die Erzeugung einer Datei geht, ist es einfacher. Hab es also mal so gemacht in Test 4 und das auch gleich noch als Ajax, gefiel mir besser, Code siehe unten.

Nun aber das Problem: Lokal auf meinem Apache läuft das alles perfekt:
-> Trage ins Formular Test 4 und 10 Sekunden ein
-> Klicke auf Start
-> Jede Sekunde wird in der Ausgabe ein Wartepunkt ergänzt
-> es erscheint die Ausgabe
Code: (dl )
1
2
Arbeit an Datei d03b46facf21182bfec9179bb2027a1f läuft, bitte warten.........
FERTIG!



Online auf dem Produktivserver passiert bei der Variante 4 folgendes:
-> Trage ins Formular Test 4 und 10 Sekunden ein
-> Klicke auf Start
-> es tut sich 10 Sekunden garnichts
-> es erscheint die Ausgabe
Code: (dl )
1
2
Arbeit an Datei 48edcea7bd579b3b47f5ba92c4bd2921 läuft, bitte warten
FERTIG!

Kein einziger Wartepunkt, wie ich das lokal korrekt habe.
Da ist doch irgendetwas noch dazwischen, das puffert, oder nicht?


Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/perl -W
use strict;
use warnings;
use IO::Handle;
STDOUT -> autoflush (1);        # sicherheitshalber statt $| = 1;
use Digest::MD5 qw (md5_hex);
use CGI qw (:push);
my $q = CGI -> new;
(my $script = $0) =~ s/^.*[\\\/]//g;
if (defined $q -> param ('action') && defined $q -> param ('sekunden')) {
        my $header = $q -> header;
        if ($q -> param ('action') eq 'start1') {
                sleep $q -> param ('sekunden');
        }
        elsif ($q -> param ('action') eq 'start3') {
                CGI -> nph (1); # no-parsed-header Modus (Header wird vom Script gesendet)
                print multipart_init (-boundary => '----here we go!');
                sleep $q -> param ('sekunden');
#               $header = "Status: 100 Continue\n$header";
# funktioniert (noch) nicht
        }
        elsif ($q -> param ('action') eq 'start4') {
                if (defined $q -> param ('warte') && $q -> param ('warte') ne '') {
                        if (-f $q -> param ('warte')) {
                                print $header . 'fertig';
                                exit ();
                        }
                        else {
                                print $header . 'hold';
                                exit ();
                        }
                }
                else {
                        my $datei = md5_hex ($ENV{'REMOTE_ADDR'},$ENV{'REMOTE_PORT'},time,ZUFALL(),$$);
                        if (my $pid = fork ()) {
                                # Vater
                                print <<HTML_TEIL;
$header
<html>
        <head>
        </head>
        <body>
                <p><div id="status">Arbeit an Datei $datei läuft, bitte warten</div></p>
                <script type="text/javascript">
                        function getXmlObject() {
                                var http_request;
                                if (window.XMLHttpRequest) {
                                        http_request = new XMLHttpRequest();
                                }
                                else
                                        if (window.ActiveXObject) {
                                                http_request = new ActiveXObject("Microsoft.XMLHTTP");
                                        }
                                return http_request;
                        }

                        function ajax_request () {
                                var http_request = getXmlObject();
                                http_request.onreadystatechange = function() {
                                        try {
                                                if (http_request.readyState == 4) {
                                                        if (http_request.status == 200) {
                                                                if (http_request.responseText == 'hold') {
                                                                        document.getElementById('status').innerHTML += '.';
                                                                        window.setTimeout(ajax_request,1000);
                                                                }
                                                                else {
                                                                        document.getElementById('status').innerHTML += '<br>FERTIG!';
                                                                }
                                                        }
                                                }
                                        }
                                        catch(e) {}
                                };
                                http_request.open("POST","$script",true);
                                http_request.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=ISO-8859-1');
                                http_request.send("action=start4&sekunden=0&warte=$datei");
                        }
                        ajax_request ();
                </script>
        </body>
</html>
HTML_TEIL
                                exit ();
                                # /Vater
                        }
                        else {
                                # Sohn
                                sleep $q -> param ('sekunden');
                                open (my $fh,">$datei");
                                print $fh "test";
                                exit ();
                                # /Sohn
                        }
                }
                sub ZUFALL {
                        srand (time ^ $$);
                        my $pw = '';
                        my @atoms = ('a'..'z', 'A'..'Z' ,0..9);
                        until ( length($pw) == 12) { $pw .= $atoms[rand scalar @atoms] }
                        return $pw;
                }
        }
        else {
                print $header;
                for (my $z = 0; $z < $q -> param ('sekunden'); $z ++) {
                        print " \n";
                        sleep 1;
                }
                $header = '';
        }
        print <<HTML_TEIL;
$header
<html>
        <head>
        </head>
        <body>
                <p> Bin DA!
                        <form action="$script" method="post">
                                <input type="submit" value="Nochmal!">
                        </form>
                </p>
        </body>
</html>
HTML_TEIL
}
else {
        print $q -> header . <<HTML_TEIL;
<html>
        <head>
        </head>
        <body>
                <p>
                        <form action="$script" method="post">
                                <select name="action">
                                        <option value="start1">Test 1 (einfaches sleep)</option>
                                        <option value="start2">Test 2 (Schleife mit Blank pro Sekunde)</option>
                                        <option value="start3">Test 3 (mit eigenem Header)</option>
                                        <option value="start4">Test 4 (mit Ajax Warteseite)</option>
                                </select><br>
                                Anzahl Sekunden: <input id="sekinp" type="text" name="sekunden"><br>
                                <input type="submit" value="Start">
                        </form>
                </p>
        </body>
</html>
HTML_TEIL
}
10 print "Hallo"
20 goto 10

View full thread Browser Keep-alive