<br><br><div class="gmail_quote">2008/4/21 Jaroslav Lukesh <<a href="mailto:lukesh@seznam.cz">lukesh@seznam.cz</a>>:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Ajax není PUSH, ale PULL</blockquote><div><br>Jde to jednoduse nasimulovat:<br><br>V JS udelejte bezny setInterval s periodou cca 1s (klidne i mene), ve volane funkci vytvarejte request na serveru.<br>V pythonu na serveru po requestu z javascriptu zkontrolujte, jestli se provedla ta akce, na kterou javascript ceka. Pokud ne, pockejte sekundu (klidne i mene) a test opakujte max x-krat, pote v nejhorsim vratte prazdny request. Jedina nevyhoda toho mechanismu je vyssi pocet connection na serveru pri paralelnim pristupu k aplikaci (request neni vyrizen behem 0.00nic sekundy, ale muze tam viset treba 10 sekund), zato efekt je super - pouzivam to u sveho ajax vieweru logu apache2.<br>
<br>Na zaver prikladam kus kodu (psano pomoci jQuery a Pythonu) pod licenci "delejte si s tim co chcete" :-) :<br><br>//funkce volana po 100ms v javascriptu, ziskava XML a parsuje do stranky<br>function checkLog() {<br>
user = $('#seluser').val()<br> log = $('#sellog').val()<br><br> if(!user || !log || working) return;<br><br> $('#status').text('reading...')<br> working = true<br> $.get('log.py/show?user='+user+'&log='+log+'&size='+size+'&filter='+$('#filter').val(), function(data) {<br>
<br> working = false<br> $('#logarea').prepend($('data', data).text())<br><br> if($('#logarea').text().length > 1000000) {<br> $('#logarea').text($('#logarea').text().substr(0,900000))<br>
}<br><br> size = $('size', data).text()<br> $('#status').text('')<br> })<br>}<br><br># funkce v pythonu resici HTTP pooling, vraci data pokud jsou, jinak ceka par sekund<br>
def show(req, user, log, size, filter):<br> global logpath<br><br> if user == '_master':<br> logfile = "%s/%s"%(logpath, log)<br> else:<br> logfile = "%s/%s/%s"%(logpath, user, log)<br>
<br> fp = open(logfile, 'r')<br><br> size = int(size)<br> if size == 0:<br> size = os.path.getsize(logfile)<br> size -= min(size, 3000)<br> fp.seek(size)<br> lines = _readlinesWait(fp, filter)<br>
del lines[0]<br> else:<br> fp.seek(size)<br> lines = _readlinesWait(fp, filter)<br><br> size = os.path.getsize(logfile)<br> fp.close()<br><br> ret = ''<br> for l in lines:<br> ret = "%s%s"%(l, ret)<br>
<br> return "<xml><size>%s</size><data>%s</data></xml>"%(size, ret)<br><br>def _readlinesWait(fp, filter):<br> tries = 5<br> while(tries):<br> tmp = fp.readlines()<br>
lines = []<br><br> for l in tmp:<br> if filter != '' and l.find(filter) == -1:<br> continue<br><br> if l.find('log.py/') == -1:<br> lines.append(l)<br>
<br> if len(lines): break<br><br> tries -= 1<br> time.sleep(1)<br><br> return lines<br><br></div></div>Snad Vam to k necemu bude prospesne...<br><br>S pozdravem<br>MP<br>-- <br>gtalk/<a href="mailto:jabber%3Aslush@jabber.cz">jabber:slush@jabber.cz</a><br>
email/<a href="mailto:msn%3Amarek@palatinus.cz">msn:marek@palatinus.cz</a><br>icq:360-737-802<br>tel:+420 724 249 422