Re: [web2py] Re: fall back for memcached to db caching

2012-12-25 Thread Adnan Smajlovic
What I wanted to do is when there is no memcache installed, or something
happens to it while running, the application should switch automatically to
db sessions, rather than just throw an error.

The code I got bellow re-creates the problem, but if I add an extra if
notsession
: session.connect(request, response, db=db) it works fine for my purposes.

Sorry that I'm explaining this upside-down :)


On Tue, Dec 25, 2012 at 1:23 AM, Massimo Di Pierro 
massimo.dipie...@gmail.com wrote:

 I am not sure I fully understand the problem.

 One issue is that you cannot do:

 try:
   redirect(url)
 except:
   something

 you have to do

 try:
   redirect(url)
 except HTTP, e:
   raise e
 except:
   db.rollback() # maybe
   something

 because a redirect will raise HTTP which extends Exception. You need to
 re-reise it for it to work.

 Massimo

 On Tuesday, 25 December 2012 00:01:19 UTC-6, Adi wrote:

 Thanks Massimo. The exception works fine, but the problem raised when
 calling a grid. Didn't realize it's not possible to connect to another
 session within the except segment. Seems that code has to be outside.
 Still learning python here :)


 db.py
 -
 try:
 from gluon.contrib.memcache import MemcacheClient
 memcache_servers = ['127.0.0.1:11211']
 cache.memcache = MemcacheClient(request, memcache_servers)
 cache.ram = cache.disk = cache.memcache
 from gluon.contrib.memdb import MEMDB
 session.connect(request,respon**se,db=MEMDB(cache.memcache))
 except Exception:
 session.connect(request, response, db=db) # doesn't work
 pass

 # had to do it this way, and then all works as expected
 if not session:
 session.connect(request, response, db=db)

 db.define_table('mytable',Fiel**d('myfield','string'))


 default.py
 --
 def gr():
 q = (db.mytable.id0)
 grid=SQLFORM.grid(q)
 return dict()









 On Monday, December 24, 2012 5:11:17 PM UTC-5, Massimo Di Pierro wrote:

 Can you try replace:

 except:

 with

 except Exception:

 On Monday, 24 December 2012 10:04:08 UTC-6, Adi wrote:

 Tried catching the exception when memcache is not available to switch
 to db caching, but getting an excepting.

 Should it be possible to catch this exception, ignore it and continue
 running?

 Thanks,
 Adnan


 try:
 from gluon.contrib.memcache import MemcacheClient
 memcache_servers = ['127.0.0.1:11211']
 cache.memcache = MemcacheClient(request, memcache_servers)
 cache.ram = cache.disk = cache.memcache
 from gluon.contrib.memdb import MEMDB
 session.connect(request,respon**se,db=MEMDB(cache.memcache))
 except:
 sys.exc_clear()
 session.connect(request, response, db=db)
 pass



 Traceback (most recent call last):
  File /Users/adnan/web2py24/gluon/**main.py, line 557, in wsgibase
  session._try_store_in_db(requ**est, response)
  File /Users/adnan/web2py24/gluon/**globals.py, line 739, 
 in_try_store_in_db
  record_id = table.insert(**dd)
  File /Users/adnan/web2py24/gluon/**contrib/memdb.py, line 256, ininsert
  id = self._create_id()
  File /Users/adnan/web2py24/gluon/**contrib/memdb.py, line 296, 
 in_create_id
  raise Exception('cannot set memcache')
 Exception: cannot set memcache



 Frames



  File /Users/adnan/web2py24/gluon/ma**in.py in wsgibase at line 557
 code arguments variables



  File /Users/spa...
 Show 
 originalhttps://groups.google.com/group/web2py/msg/154fffb31c2a7321?dmode=sourceoutput=gplainnoredirect

  --





-- 





[web2py] Re: fall back for memcached to db caching

2012-12-24 Thread Massimo Di Pierro
Can you try replace:

except:

with

except Exception:

On Monday, 24 December 2012 10:04:08 UTC-6, Adi wrote:

 Tried catching the exception when memcache is not available to switch to 
 db caching, but getting an excepting. 

 Should it be possible to catch this exception, ignore it and continue 
 running? 

 Thanks,
 Adnan


 try:
 from gluon.contrib.memcache import MemcacheClient
 memcache_servers = ['127.0.0.1:11211']
 cache.memcache = MemcacheClient(request, memcache_servers)
 cache.ram = cache.disk = cache.memcache
 from gluon.contrib.memdb import MEMDB
 session.connect(request,response,db=MEMDB(cache.memcache))
 except:
 sys.exc_clear()
 session.connect(request, response, db=db)
 pass



 Traceback (most recent call last):
  File /Users/adnan/web2py24/gluon/main.py, line 557, in wsgibase
  session._try_store_in_db(request, response)
  File /Users/adnan/web2py24/gluon/globals.py, line 739, in_try_store_in_db
  record_id = table.insert(**dd)
  File /Users/adnan/web2py24/gluon/contrib/memdb.py, line 256, in insert
  id = self._create_id()
  File /Users/adnan/web2py24/gluon/contrib/memdb.py, line 296, in_create_id
  raise Exception('cannot set memcache')
 Exception: cannot set memcache



 Frames 
  
  

  File /Users/adnan/web2py24/gluon/main.py in wsgibase at line 557 code 
 arguments variables 
  
  

  File /Users/adnan/web2py24/gluon/globals.py in _try_store_in_db at line 
 739 code arguments variables 
  
  

  File /Users/adnan/web2py24/gluon/contrib/memdb.py in insert at line 256code 
 arguments variables 
  
  

  File /Users/adnan/web2py24/gluon/contrib/memdb.py in _create_id at line 
 296 code arguments variables 
  
 Function argument list 

 (self=DALStorage {'client_ip': gluon.contrib.memdb.F...luon.contrib.
 memdb.Field object at 0x111d554d0}) 
  
 Code listing 
 291.
 292.
 span style=color: #066; class=styled-by-prettify...
 Show 
 originalhttps://groups.google.com/group/web2py/msg/dc3f7fc28bb3d0c5?dmode=sourceoutput=gplainnoredirect


-- 





[web2py] Re: fall back for memcached to db caching

2012-12-24 Thread Adi
Thanks Massimo. The exception works fine, but the problem raised when 
calling a grid. Didn't realize it's not possible to connect to another 
session within the except segment. Seems that code has to be outside. 
Still learning python here :)


db.py
-
try:
from gluon.contrib.memcache import MemcacheClient
memcache_servers = ['127.0.0.1:11211']
cache.memcache = MemcacheClient(request, memcache_servers)
cache.ram = cache.disk = cache.memcache
from gluon.contrib.memdb import MEMDB
session.connect(request,response,db=MEMDB(cache.memcache))
except Exception:
session.connect(request, response, db=db) # doesn't work
pass

# had to do it this way, and then all works as expected
if not session:
session.connect(request, response, db=db)

db.define_table('mytable',Field('myfield','string'))


default.py
--
def gr():
q = (db.mytable.id0)
grid=SQLFORM.grid(q)
return dict()









On Monday, December 24, 2012 5:11:17 PM UTC-5, Massimo Di Pierro wrote:

 Can you try replace:

 except:

 with

 except Exception:

 On Monday, 24 December 2012 10:04:08 UTC-6, Adi wrote:

 Tried catching the exception when memcache is not available to switch to 
 db caching, but getting an excepting. 

 Should it be possible to catch this exception, ignore it and continue 
 running? 

 Thanks,
 Adnan


 try:
 from gluon.contrib.memcache import MemcacheClient
 memcache_servers = ['127.0.0.1:11211']
 cache.memcache = MemcacheClient(request, memcache_servers)
 cache.ram = cache.disk = cache.memcache
 from gluon.contrib.memdb import MEMDB
 session.connect(request,response,db=MEMDB(cache.memcache))
 except:
 sys.exc_clear()
 session.connect(request, response, db=db)
 pass



 Traceback (most recent call last):
  File /Users/adnan/web2py24/gluon/main.py, line 557, in wsgibase
  session._try_store_in_db(request, response)
  File /Users/adnan/web2py24/gluon/globals.py, line 739, in_try_store_in_db
  record_id = table.insert(**dd)
  File /Users/adnan/web2py24/gluon/contrib/memdb.py, line 256, in insert
  id = self._create_id()
  File /Users/adnan/web2py24/gluon/contrib/memdb.py, line 296, in_create_id
  raise Exception('cannot set memcache')
 Exception: cannot set memcache



 Frames 
  
  

  File /Users/adnan/web2py24/gluon/main.py in wsgibase at line 557 code 
 arguments variables 
  
  

  File /Users/adnan/web2py24/gluon/globals.py in _try_store_in_db at line 
 739 code arguments variables 
  
  

  File /Users/adnan/web2py24/gluon/contrib/memdb.py in insert at line 256code 
 arguments variables 
  
  

  File /Users/adnan/web2py24/gluon/contrib/memdb.py in _create_id at line 
 296 code arguments variables 
  
 Function argument list 

 (self=DALStorage {'client_ip': gluon.contrib.memdb.F...luon.contrib.
 memdb.Field object at 0x111d554d0}) 
  
 Code listing 
 291.
 292.
 span style=color: #066; class=styled-by-prettify...
 Show 
 originalhttps://groups.google.com/group/web2py/msg/dc3f7fc28bb3d0c5?dmode=sourceoutput=gplainnoredirect



-- 





[web2py] Re: fall back for memcached to db caching

2012-12-24 Thread Massimo Di Pierro
I am not sure I fully understand the problem.

One issue is that you cannot do:

try:
  redirect(url)
except:
  something

you have to do

try:
  redirect(url)
except HTTP, e:
  raise e
except:
  db.rollback() # maybe
  something

because a redirect will raise HTTP which extends Exception. You need to 
re-reise it for it to work.

Massimo

On Tuesday, 25 December 2012 00:01:19 UTC-6, Adi wrote:

 Thanks Massimo. The exception works fine, but the problem raised when 
 calling a grid. Didn't realize it's not possible to connect to another 
 session within the except segment. Seems that code has to be outside. 
 Still learning python here :)


 db.py
 -
 try:
 from gluon.contrib.memcache import MemcacheClient
 memcache_servers = ['127.0.0.1:11211']
 cache.memcache = MemcacheClient(request, memcache_servers)
 cache.ram = cache.disk = cache.memcache
 from gluon.contrib.memdb import MEMDB
 session.connect(request,response,db=MEMDB(cache.memcache))
 except Exception:
 session.connect(request, response, db=db) # doesn't work
 pass

 # had to do it this way, and then all works as expected
 if not session:
 session.connect(request, response, db=db)

 db.define_table('mytable',Field('myfield','string'))


 default.py
 --
 def gr():
 q = (db.mytable.id0)
 grid=SQLFORM.grid(q)
 return dict()









 On Monday, December 24, 2012 5:11:17 PM UTC-5, Massimo Di Pierro wrote:

 Can you try replace:

 except:

 with

 except Exception:

 On Monday, 24 December 2012 10:04:08 UTC-6, Adi wrote:

 Tried catching the exception when memcache is not available to switch to 
 db caching, but getting an excepting. 

 Should it be possible to catch this exception, ignore it and continue 
 running? 

 Thanks,
 Adnan


 try:
 from gluon.contrib.memcache import MemcacheClient
 memcache_servers = ['127.0.0.1:11211']
 cache.memcache = MemcacheClient(request, memcache_servers)
 cache.ram = cache.disk = cache.memcache
 from gluon.contrib.memdb import MEMDB
 session.connect(request,response,db=MEMDB(cache.memcache))
 except:
 sys.exc_clear()
 session.connect(request, response, db=db)
 pass



 Traceback (most recent call last):
  File /Users/adnan/web2py24/gluon/main.py, line 557, in wsgibase
  session._try_store_in_db(request, response)
  File /Users/adnan/web2py24/gluon/globals.py, line 739, in_try_store_in_db
  record_id = table.insert(**dd)
  File /Users/adnan/web2py24/gluon/contrib/memdb.py, line 256, ininsert
  id = self._create_id()
  File /Users/adnan/web2py24/gluon/contrib/memdb.py, line 296, in_create_id
  raise Exception('cannot set memcache')
 Exception: cannot set memcache



 Frames 
  
  

  File /Users/adnan/web2py24/gluon/main.py in wsgibase at line 557 code 
 arguments variables 
  
  

  File /Users/spa...
 Show 
 originalhttps://groups.google.com/group/web2py/msg/154fffb31c2a7321?dmode=sourceoutput=gplainnoredirect



--