There's a simple workaround for this problem. I've tested it and it
works fine. The concept is that I have to make the ajax request
synchronous. For this purpose I've used the jQuery.ajax function
instead of the web2py supplied one and I had to put everythin inside a
queue. The code in my first post was rewritten like this:

function dvd() {
        $.ajax({
                async: false,
                url: 'bg_quick_dvd',
                data: {
                        keyword: $('#keyword').val(),
                        option: $('#option').val()
                },
                type: 'POST',
                success: function(data){
                        $('div#target_dvd').html(data);
                }
        })
        queue_start();
}
function music_cd() {
        $.ajax({
                async: false,
                url: 'bg_quick_music_cd',
                data: {
                        keyword: $('#keyword').val(),
                        option: $('#option').val()
                },
                type: 'POST',
                success: function(data){
                        $('div#target_music_cd').html(data);
                }
        })
        queue_start();
}
function book() {
        $.ajax({
                async: false,
                url: 'bg_quick_book',
                data: {
                        keyword: $('#keyword').val(),
                        option: $('#option').val()
                },
                type: 'POST',
                success: function(data){
                        $('div#target_book').html(data);
                }
        })
}

var queue = [];
function start() {
        if(jQuery('#title').attr('checked'))
                jQuery('#option').val('1');
        if(jQuery('#store').attr('checked'))
                jQuery('#option').val('0');

        queue.push(book);
        queue.push(music_cd);
        queue.push(dvd);
        queue_start();
}

function queue_start() {
        if(queue.length > 0) {
                var fn = queue.pop();
                fn();
        }
}




On Jun 24, 7:14 pm, ScOut3R <mailingl...@modernbiztonsag.org> wrote:
> Dear List,
>
> theajaxbasedbackgroundsearch crashes my web2py installation. It's
> version 1.77.3 and I'm using the fcgihandler to access the
> application. OS: OpenBSD 4.6, Python version is Python 2.5.4
> (r254:67916, Oct 19 2009, 01:52:14).
>
> As You can see below the search makes three database queries. The
> first search attempt works and the second crashes web2py. Would You be
> so kind to look into it?
>
> The error is the following:
>
> Unhandled exception in thread started by <bound method Connection.run
> of <gluon.contrib.gateways.fcgi.Connection object at 0x8b5af36c>>
> Traceback (most recent call last):
>   File "/var/www/web2py/prod/gluon/contrib/gateways/fcgi.py", line
> 664, in run
>     self.process_input()
>   File "/var/www/web2py/prod/gluon/contrib/gateways/fcgi.py", line
> 700, in process_input
>     self._do_params(rec)
>   File "/var/www/web2py/prod/gluon/contrib/gateways/fcgi.py", line
> 799, in _do_params
>     self._start_request(req)
>   File "/var/www/web2py/prod/gluon/contrib/gateways/fcgi.py", line
> 783, in _start_request
>     req.run()
>   File "/var/www/web2py/prod/gluon/contrib/gateways/fcgi.py", line
> 592, in run
>     self._flush()
>   File "/var/www/web2py/prod/gluon/contrib/gateways/fcgi.py", line
> 599, in _flush
>     self.stdout.close()
>   File "/var/www/web2py/prod/gluon/contrib/gateways/fcgi.py", line
> 358, in close
>     self._conn.writeRecord(rec)
>   File "/var/www/web2py/prod/gluon/contrib/gateways/fcgi.py", line
> 715, in writeRecord
>     rec.write(self._sock)
>   File "/var/www/web2py/prod/gluon/contrib/gateways/fcgi.py", line
> 552, in write
>     self._sendall(sock, header)
>   File "/var/www/web2py/prod/gluon/contrib/gateways/fcgi.py", line
> 529, in _sendall
>     sent = sock.send(data)
> socket.error: (32, 'Broken pipe')
>
> The jquery snippet that calls thebackgroundfunctions:
>
> function dvd() {ajax('bg_quick_dvd', ['keyword', 'option'],
> 'target_dvd'); }
> function music_cd() {ajax('bg_quick_music_cd', ['keyword', 'option'],
> 'target_music_cd'); }
> function book() {ajax('bg_quick_book', ['keyword', 'option'],
> 'target_book');
>
> function start() {
>         if(jQuery('#title').attr('checked'))
>                 jQuery('#option').val('1');
>         if(jQuery('#store').attr('checked'))
>                 jQuery('#option').val('0');
>
>         dvd();
>         setTimeout('music_cd()', 150);
>         setTimeout('book()', 300);
>
> }
>
> And the backround functions:
>
> def bg_quick_dvd():
>         if (request.vars.option == '1'):
>                 pattern = '%' + request.vars.keyword + '%'
>                 dvds = db((db.dvds.user==user_id) &
> (db.dvds.title.like(pattern))).select(orderby=db.dvds.title)
>         else:
>                 dvds = db((db.dvds.user==user_id) &
> (db.dvds.store==request.vars.keyword)).select(orderby=db.dvds.title)
>         items = [A(row.title, _href=URL(c='dvd', r=request, f='show',
> args=row.id)) for row in dvds]
>         return UL(*items).xml()
>
> @auth.requires_login()
> def bg_quick_music_cd():
>         if (request.vars.option == '1'):
>                 pattern = '%' + request.vars.keyword + '%'
>                 music_cds = db((db.music_cds.user==user_id) &
> (db.music_cds.title.like(pattern))).select(orderby=db.music_cds.title)
>         else:
>                 music_cds = db((db.music_cds.user==user_id) &
> (db.music_cds.store==request.vars.keyword)).select(orderby=db.music_cds.tit 
> le)
>         items = [A(row.title, _href=URL(c='music_cd', r=request, f='show',
> args=row.id)) for row in music_cds]
>         return UL(*items).xml()
>
> @auth.requires_login()
> def bg_quick_book():
>         if (request.vars.option == '1'):
>                 pattern = '%' + request.vars.keyword + '%'
>                 books = db((db.books.user==user_id) &
> (db.books.title.like(pattern))).select(orderby=db.books.title)
>         else:
>                 books = db((db.books.user==user_id) &
> (db.books.store==request.vars.keyword)).select(orderby=db.books.title)
>         items = [A(row.title, _href=URL(c='book', r=request, f='show',
> args=row.id)) for row in books]
>         return UL(*items).xml()
>
> Best regards,
> Mate

Reply via email to