db select cache.ram.clear() and apache2 + wsgi not working

31 views
Skip to first unread message

szimszon

unread,
Feb 26, 2010, 2:34:41 AM2/26/10
to web2py-users
Hello!

I wonder if somebody could it confirm:

I have a db:
------- cut --------
db.define_table('guestbook',
Field('email','string',
label=T("E-Mail"),
requires=IS_EMAIL()),
Field('nick','string',
label=T("Nickname"),
requires=IS_NOT_EMPTY()),
Field('ts','datetime',
label=T("Date"),
default=request.now,
writable=False,
readable=False),
Field('text','string',
label=T("Text"),
requires=IS_NOT_EMPTY()),
)
------- cut --------

A controller:
------- cut --------
def flush_cache(form=None):
cache.ram.clear()


def vendegkonyv():
lista=None
if (( auth.user_id==1 ) and (request.vars.r)):

form=SQLFORM(db.guestbook,request.vars.r,deletable=True,onaccept=flush_cache)
else:
form=SQLFORM(db.guestbook,onaccept=flush_cache)
#form=SQLFORM.factory()
if not auth.user_id:
form[0].append(Recaptcha(request,
"...",
"..."))
if form.accepts(request.vars, session):
response.flash = "Record updated"
flush_cache()
elif form.errors:
response.flash = "Error"
flush_cache()

try:
lista=db(db.guestbook.id>0).select(orderby=~db.guestbook.ts,
cache=(cache.ram,120))
except:
pass
return dict(form=form,lista=lista)
------- cut --------

And a view:
------- cut --------
{{extend 'layout.html'}}

<h1>GuestBook</h1>
<div>
{{=form}}
</div>
{{if lista:}}
<table class="vendegkonyv">
<tr>
<th>{{=db.guestbook.ts.label}}</th>
<th>{{=db.guestbook.nick.label}}</th>
<th>{{=db.guestbook.text.label}}</th>
</tr>
{{for elem in lista:}}
<tr>
<td>
{{if (auth.user_id==1):}}

{{=A(elem.ts,_href=URL(r=request,f='vendegkonyv',vars=dict(r=elem.id)))}}
{{else:}}
{{=elem.ts}}
{{pass}}
</td>
<td>{{=elem.nick}}</td>
<td>{{=elem.text}}</td>
</tr>
{{pass}}
</table>
------- cut --------

With the cherrypy isn't a problem but with apache2 and wsgi:

1, I make 3-4 post to the guest book
2, delete them
3, reload the page with a link on page pointing to self to guestbook

I see 0 to 3-4 post on each click randomly which are nonexistent posts
anymore :(

mdipierro

unread,
Feb 26, 2010, 3:10:26 AM2/26/10
to web2py-users
The fact is the web server may be restarting the process or running
more than one process. Apache does the same. In this case you should
use cache.disk, not cache.ram.

szimszon

unread,
Feb 26, 2010, 3:32:00 AM2/26/10
to web2py-users
Okay. It looks good now. Thanks for the prompt replay.

But if I understand well than the cache.ram is useless all the time
apache is used?

mdipierro

unread,
Feb 26, 2010, 3:42:07 AM2/26/10
to web2py-users
it is not useless. It works of if you run only one wsgi process but
cache lives only within the lifetime of the process.

szimszon

unread,
Feb 26, 2010, 3:44:49 AM2/26/10
to web2py-users
I see, thanks.

Graham Dumpleton

unread,
Feb 26, 2010, 5:25:38 AM2/26/10
to web2py-users
For a further explanation of different modes you can run application
under with Apache/mod_wsgi read:

http://code.google.com/p/modwsgi/wiki/ProcessesAndThreading

Graham

szimszon

unread,
Feb 26, 2010, 8:07:58 AM2/26/10
to web2py-users
Nice. TNX.

On febr. 26, 11:25, Graham Dumpleton <graham.dumple...@gmail.com>
wrote:

Thadeus Burgess

unread,
Feb 26, 2010, 10:50:04 AM2/26/10
to web...@googlegroups.com
Cache.ram is not "completely" useless in a multi processes. If your
caching the entire html output of your front page, then its faster to
have this cached in ram instead of on disk, but its not a big deal
since its ok for new threads to recreate the cache in this single
instance.

Also if your running on latest stable web2py code, it does include
some cache statistics in appadmin, including memory usage if you have
guppy heapy installed

-Thadeus

> --
> You received this message because you are subscribed to the Google Groups "web2py-users" group.
> To post to this group, send email to web...@googlegroups.com.
> To unsubscribe from this group, send email to web2py+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/web2py?hl=en.
>
>

szimszon

unread,
Mar 11, 2010, 5:07:09 AM3/11/10
to web2py-users
Hm... After time I see that cache.disk.clear() isn't working with my
wsgi setup too :(

WSGIDaemonProcess web2py user=www-data group=www-data home=/usr/local/
web2py/ processes=5 maximum-requests=10000

Need more testing to allocate the problem :-o


On febr. 26, 09:10, mdipierro <mdipie...@cs.depaul.edu> wrote:

mdipierro

unread,
Mar 11, 2010, 10:03:53 AM3/11/10
to web2py-users
very odd. This is the function gluon/cache.py that is supposed to do
the clearing:

def clear(self, regex=None):
locker = open(self.locker_name,'a')
portalocker.lock(locker, portalocker.LOCK_EX)
storage = shelve.open(self.shelve_name)
if regex == None:
storage.clear()
else:
self._clear(storage, regex)
if not CacheAbstract.cache_stats_name in storage.keys():
storage[CacheAbstract.cache_stats_name] = {
'hit_total': 0,
'misses': 0,
}
storage.sync()
portalocker.unlock(locker)
locker.close()

szimszon

unread,
Mar 16, 2010, 11:57:34 AM3/16/10
to web2py-users
Oh, sorry my fault I had a cache.ram left in a select() sorry...
cache.disk seems to work correct :-o

Sorry.

On márc. 11, 16:03, mdipierro <mdipie...@cs.depaul.edu> wrote:
> very odd. This is the function gluon/cache.py that is supposed to do
> the clearing:
>

>     defclear(self, regex=None):


>         locker = open(self.locker_name,'a')
>         portalocker.lock(locker, portalocker.LOCK_EX)
>         storage = shelve.open(self.shelve_name)
>         if regex == None:
>             storage.clear()
>         else:
>             self._clear(storage, regex)
>         if not CacheAbstract.cache_stats_name in storage.keys():
>             storage[CacheAbstract.cache_stats_name] = {
>                 'hit_total': 0,
>                 'misses': 0,
>             }
>         storage.sync()
>         portalocker.unlock(locker)
>         locker.close()
>
> On Mar 11, 4:07 am, szimszon <szims...@gmail.com> wrote:
>
>
>

> > Hm... After time I see thatcache.disk.clear() isn't working with my

Reply all
Reply to author
Forward
0 new messages