On 1/15/14 7:13 AM, Asaf Las wrote:
Hi community

i am beginner in Python and have possibly silly questions i could not figure 
out answers for.

Below is the test application working with uwsgi to test json-rpc.
------------------------------------------------------------------------
from multiprocessing import Process
from werkzeug.wrappers import Request, Response
from werkzeug.serving import run_simple

from jsonrpc import JSONRPCResponseManager, dispatcher

p = "module is loaded"   <------ (3)
print(p)
print(id(p))

@Request.application
def application(request):
     print("server started")

     print(id(p))

     # Dispatcher is dictionary {<method_name>: callable}
     dispatcher["echo"] = lambda s: s                   <---- (1)
     dispatcher["add"] = lambda a, b: a + b             <---- (2)

     print("request data ==> ", request.data)
     response = JSONRPCResponseManager.handle(request.data, dispatcher)
     return Response(response.json, mimetype='application/json')
------------------------------------------------------------------------

As program will grow new rpc method dispatchers will be added so there is idea 
to reduce initialization code at steps 1 and 2 by making them global objects 
created at module loading, like string p at step 3.

Multithreading will be enabled in uwsgi and 'p' will be used for read only.

The important concepts to understand are names and values. All values in Python work the same way: they live until no name refers to them. Also, any name can be assigned to (rebound) after it has been defined.

This covers the details in more depth: http://nedbatchelder.com/text/names.html


Questions are:
- what is the lifetime for global object (p in this example).

The name p is visible in this module for as long as the program is running. The object you've assigned to p can be shorter-lived if p is reassigned.

- will the p always have value it got during module loading

Depends if you reassign it.

- if new thread will be created will p be accessible to it

If the thread is running code defined in this module, yes, that code will be able to access p in that thread.

- if p is accessible to new thread will new thread initialize p value again?

No, the module is only imported once, so the statements at the top level of the module are only executed once.

- is it guaranteed to have valid p content (set to "module is loaded") whenever 
application() function is called.

Yes, unless you reassign p.

- under what condition p is cleaned by gc.

Names are not reclaimed by the garbage collector, values are. The value assigned to p can be reclaimed if you reassign the name p, and nothing else is referring to the value.


The rationale behind these question is to avoid object creation within 
application() whose content is same and do not change between requests calling 
application() function and thus to reduce script response time.

Thanks in advance!

Welcome.


--
Ned Batchelder, http://nedbatchelder.com

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to