Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #63979

Re: Question about object lifetime and access

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'beginner': 0.05; 'initialize': 0.07; 'referring': 0.07; 'subject:Question': 0.07; 'welcome.': 0.07; 'string': 0.09; 'executed': 0.09; 'imported': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'statements': 0.09; 'way:': 0.09; 'python': 0.11; 'def': 0.12; 'thread': 0.14; 'advance!': 0.16; 'called.': 0.16; 'cleaned': 0.16; 'defined.': 0.16; 'example).': 0.16; 'garbage': 0.16; 'lambda': 0.16; 'once.': 0.16; 'rationale': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'silly': 0.16; 'subject:access': 0.16; 'subject:lifetime': 0.16; 'subject:object': 0.16; 'wrote:': 0.18; 'module': 0.19; 'value.': 0.19; 'import': 0.22; 'header :User-Agent:1': 0.23; 'module,': 0.24; 'received:comcast.net': 0.24; 'refers': 0.24; 'visible': 0.24; 'question': 0.24; 'script': 0.25; 'possibly': 0.26; 'defined': 0.27; 'values': 0.27; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'function': 0.29; 'am,': 0.29; 'thus': 0.29; 'code': 0.31; 'requests': 0.31; 'are.': 0.31; 'enabled': 0.31; 'loading': 0.31; 'once,': 0.31; 'request,': 0.31; 'values.': 0.31; 'figure': 0.32; 'community': 0.33; 'running': 0.33; 'are:': 0.33; 'could': 0.34; 'created': 0.35; 'no,': 0.35; 'objects': 0.35; 'test': 0.35; 'there': 0.35; 'accessible': 0.36; 'skip:j 20': 0.36; 'method': 0.36; 'thanks': 0.36; 'behind': 0.37; 'application': 0.37; 'las': 0.37; 'level': 0.37; 'step': 0.37; 'depends': 0.38; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'read': 0.60; 'new': 0.61; "you've": 0.63; 'making': 0.63; 'name': 0.63; 'more': 0.64; 'details': 0.65; 'within': 0.65; 'between': 0.67; 'covers': 0.68; 'guaranteed': 0.75; 'grow': 0.77; 'again?': 0.84; 'jsonrpc': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ned Batchelder <ned@nedbatchelder.com>
Subject Re: Question about object lifetime and access
Date Wed, 15 Jan 2014 07:55:48 -0500
References <30012227-c0f5-4bbe-9e6c-d333cd5682a2@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host c-50-133-228-126.hsd1.ma.comcast.net
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.2.0
In-Reply-To <30012227-c0f5-4bbe-9e6c-d333cd5682a2@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5508.1389790566.18130.python-list@python.org> (permalink)
Lines 86
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1389790566 news.xs4all.nl 2859 [2001:888:2000:d::a6]:53167
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:63979

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Question about object lifetime and access Asaf Las <roegltd@gmail.com> - 2014-01-15 04:13 -0800
  Re: Question about object lifetime and access Ned Batchelder <ned@nedbatchelder.com> - 2014-01-15 07:55 -0500
  Re: Question about object lifetime and access Chris Angelico <rosuav@gmail.com> - 2014-01-16 00:05 +1100
    Re: Question about object lifetime and access Asaf Las <roegltd@gmail.com> - 2014-01-15 05:25 -0800
  Re: Question about object lifetime and access Asaf Las <roegltd@gmail.com> - 2014-01-15 05:14 -0800
    Re: Question about object lifetime and access Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-01-16 00:02 +0000
      Re: Question about object lifetime and access Asaf Las <roegltd@gmail.com> - 2014-01-15 19:50 -0800
  Re:Question about object lifetime and access Dave Angel <davea@davea.name> - 2014-01-15 10:11 -0500

csiph-web