×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    47

    MWS use persistent variables in python

    Hi,

    I was wondering if its possible to use persistent variables in a mod_python handler.

    What I mean is a variable that is stored between two user requests, for example a counter that increments every time a user access the page, although that is not what I intend to do .

    I tried this here:
    Code:
    global count
    
    def handler(req):
    	from mod_python import apache
    	
    	try:
    		# Should be execute if variable is defined in memory 
    		req.write("count: " + str(count))
    		count = count + 1
    	except Exception, e:
    		# If variable is not bound an exception is thrown, then this is called
    		count = 0
    		
    	req.write("count: " + str(count))
    	
    	return apache.OK
    but it always just writes out:
    count: 0

    but not count: 0 after the first time, count: 0 and count: 1 after the next and count: 1 and count: 2 after the third ... as I want.

    If any one knows how to do that please let me know. I want to try to use something like this for setting up an environment (reading stuff from database and stuff like that) just one time and store it in a persistent variable for fast access for subsequent requests.

    Best regards,
    Magnus

  2. #2
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    Re: MWS use persistent variables in python

    It seems that global variables are understood incorrectly. Here's an example how they work and how they can be used.

    Code:
    def handler(req):
    	global count
    	from mod_python import apache
    	
    	try:
    		# Should be execute if variable is defined in memory 
    		req.write("count: " + str(count))
    		count = count + 1
    	except NameError, e:
    		# If variable is not bound an exception is thrown, then this is called
    		count = 0
    		
    	req.write("count: " + str(count))
    	
    	return apache.OK
    If this doesn't help please tell more details about your problems.

Similar Threads

  1. Please help installing Python libraries on S60
    By ericroijen in forum Symbian
    Replies: 11
    Last Post: 2009-07-18, 10:43
  2. New to Python for S60 (PyS60)? Read this first!
    By croozeus in forum Nokia N9
    Replies: 2
    Last Post: 2008-10-17, 19:23
  3. Replies: 1
    Last Post: 2008-01-08, 09:58
  4. Replies: 11
    Last Post: 2007-10-03, 19:46
  5. Executing a Python script at MWS startup
    By cristiano.diflora in forum Mobile Web Server
    Replies: 1
    Last Post: 2007-09-24, 13:59

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×