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


Groups > comp.lang.python > #102501

Re: How a module is being marked as imported?

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Kevin Conway <kevinjacobconway@gmail.com>
Newsgroups comp.lang.python
Subject Re: How a module is being marked as imported?
Date Thu, 04 Feb 2016 16:11:09 +0000
Lines 57
Message-ID <mailman.67.1454602281.30993.python-list@python.org> (permalink)
References <325bc056-0683-408b-85ba-6c3cd0f69d41@googlegroups.com> <dc36625d-ecd1-4f4d-be26-810f92c4ee54@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de AK88BD+SC0Y8wXW17GN4uAujkhp3CmHySqPofs0P88ZA==
Return-Path <kevinjacobconway@gmail.com>
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; 'received:209.85.223': 0.03; 'heavily': 0.04; 'cpython': 0.05; 'raises': 0.07; 'subject:How': 0.09; 'boundaries.': 0.09; 'cached': 0.09; 'imports': 0.09; 'subject:module': 0.09; 'thread': 0.10; 'python': 0.10; 'exception': 0.13; 'thursday,': 0.13; '(but': 0.15; 'acquired': 0.15; 'explicitly': 0.15; 'interpreter': 0.15; 'thu,': 0.15; '(line': 0.16; '2016': 0.16; 'enough.': 0.16; 'gil,': 0.16; 'loaded.': 0.16; 'modules),': 0.16; 'placeholder': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'reproduce': 0.16; 'sys.modules': 0.16; 'threads': 0.16; 'wrote:': 0.16; 'later': 0.16; 'attribute': 0.18; 'mechanism': 0.18; 'pfxlen:0': 0.18; '&gt;': 0.18; 'email addr:gmail.com&gt;': 0.18; 'solution.': 0.18; 'language': 0.19; 'all,': 0.20; 'to:2**1': 0.21; 'feb': 0.23; 'tried': 0.24; 'import': 0.24; 'patch': 0.24; 'header:In-Reply- To:1': 0.24; 'module': 0.25; "i've": 0.25; "doesn't": 0.26; 'wonder': 0.27; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; "skip:' 10": 0.28; 'behaviour': 0.29; 'does,': 0.29; 'far,': 0.29; 'gil': 0.29; 'occurred': 0.29; 'environment': 0.29; "i'm": 0.30; 'url:mailman': 0.30; 'code': 0.30; 'guess': 0.31; 'probably': 0.31; 'another': 0.32; 'noticed': 0.32; 'posting': 0.32; 'embedded': 0.32; 'statement': 0.32; 'problem': 0.33; 'url:python': 0.33; 'driven': 0.33; 'lock': 0.33; 'raised': 0.33; 'though.': 0.33; 'version:': 0.33; 'windows.': 0.33; "i'll": 0.33; 'similar': 0.33; 'url:listinfo': 0.34; 'correctly': 0.34; 'running': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'attempt': 0.35; 'c++': 0.35; 'question,': 0.35; 'sometimes': 0.35; 'project': 0.36; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'modules': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'done.': 0.37; 'thought': 0.37; 'late': 0.38; 'received:209': 0.38; 'several': 0.38; 'or,': 0.38; 'does': 0.39; 'application': 0.39; 'resources': 0.39; 'url:mail': 0.40; 'to:addr:python.org': 0.40; 'where': 0.40; 'mark': 0.40; 'some': 0.40; 'your': 0.60; 'avoid': 0.61; '&amp;': 0.61; 'per': 0.62; 'skip:n 10': 0.62; 'making': 0.62; 'above,': 0.63; 'sharing': 0.64; 'due': 0.65; 'acts': 0.72; 'race': 0.72; 'lack': 0.76; 'acquiring': 0.84; 'calls,': 0.84; 'does?': 0.84; 'subject:being': 0.84; 'maybe,': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :content-type; bh=70WYCRCyGaOyyTNSrN9buPTzhouMnNADupAtHBnW/WU=; b=yDApzT6STm3KkVqXkrA0K6MJQky2l69AaOapKOG37kzhSsqNBooToqsd1OUR9crvO0 oz5ADen0OI1Ol26UxYfbN/u13OYPD9WT579rlXFRP6sw7MF5D7ghnBTdzbK8EOkDqoaz Wh8EAdZlE+SAueFjGQ5ukRZXjOTsQ85nmkmMzmHpyTZZ52sVvcWI/GsISP/SLHaBqNWN JhICCLJ7gavhq1G8WPHepoKO4TtZ6oBcOq7YfowlsU0kO6dCaA0VqvTPYwDCRgfB5lyG lxs2igzCTHTYtRbbcKQm10M23SNQjxLpShisHigTS2G2DPVDJeP5dskpVJC1bBWNorBP uV+w==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:content-type; bh=70WYCRCyGaOyyTNSrN9buPTzhouMnNADupAtHBnW/WU=; b=YqKOkFcl9sZKWegGiYBrJkyjQaEmpQVRNXH1p1aXbanAg1vStD6MGgVx1GHr2nKkp0 b63QGJfeZAxjztTUJRtEH4889E/Wx1iD7Actcaf6t3dAx5goKv6klyDjsEmQKwuwcQdV 0+2upqmhewMEZf9grlzaQDB/iQQFcKOGAST+olM/0zK5fwadZLUa16MYQ3xJyWGcrHxH BkFXOiv1gmV+WWMgOfb1GPPbBZ2uNW80X9olHYUokuPfbu60u073rzDXjUvQ0LKgfkzL isMVvy+9x8pOa1ihvIvAOYHBoQMAkIOo8Oz/yNmKX09tz3S5XMnaYG9hM9HXnMNKMrrY tPUw==
X-Gm-Message-State AG10YOSGv0kY5zV0xZYw6Qb/ORbhuwW3Muu0tRNKdLXzxTHISKgEUOoRE4en924PUoLy39hWy60Sld7FjX9Rjg==
X-Received by 10.107.35.65 with SMTP id j62mr9724492ioj.30.1454602278607; Thu, 04 Feb 2016 08:11:18 -0800 (PST)
In-Reply-To <dc36625d-ecd1-4f4d-be26-810f92c4ee54@googlegroups.com>
X-Content-Filtered-By Mailman/MimeDel 2.1.21rc1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21rc1
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>
Xref csiph.com comp.lang.python:102501

Show key headers only | View raw


As an attempt to answer your original question, Python doesn't explicitly
mark a module as done. It does keep imports cached in sys.modules, though.
The behaviour you describe where later imports get the same module object
is driven by that cache.

There are cases, such as cyclical imports, where the object in sys.modules
is empty for a period of time and acts as a placeholder until the code from
that module is loaded. During that time, any usage of the module would
result in a similar failure to what you have described.

Additionally, if you are writing multithreaded code then acquiring the GIL
may not be enough. There is another lock used for imports.

On Thu, Feb 4, 2016, 09:43 Jean-Charles Lefebvre <polyvertex@gmail.com>
wrote:

> So far, I've been advised to:
>
> 1/ Double-check that the GIL was correctly acquired
> 2/ Ensure there's no 'string' module in my project
> 3/ Manually pre-import commonly used standard modules at interpreter's
> init-time to avoid race conditions due to the multi-threaded nature of the
> running environment
>
> No problem found for 1/ & 2/ (double-checked). I tried 3/ before posting
> and could not reproduce the problem at all which is probably the patch I
> will apply due to the lack of a better solution. I guess I'll have to dig
> into __import__'s code and related.
>
>
> On Thursday, February 4, 2016 at 11:20:05 AM UTC+1, Jean-Charles Lefebvre
> wrote:
> > Hi all,
> >
> > The short version: How CPython marks a module as being fully imported,
> if it does, so that the same import statement ran from another C thread at
> the same time does not collide? Or, reversely, does not think the module is
> not already fully imported?
> >
> > The full version: I'm running CPython 3.5.1, embedded into a C++
> application on Windows. The application is heavily multi-threaded so
> several C threads call some Python code at the same time (different Python
> modules), sharing interpreter's resources by acquiring/releasing the GIL
> frequently DURING the calls, at language boundaries.
> >
> > Sometimes (but always only once per application instance), a call to
> os.path.expandvars raises the AttributeError exception with message: module
> 'string' has no attribute 'ascii_letters'. It is raised by the
> ntpath.expandvars function (line 372). When I noticed the late import
> statement of the 'string' module at the line above, I thought that MAYBE,
> it could be because the interpreter is ran in an heavily multi-threaded
> environment and that the GIL acquiring/releasing occurred at a bad timing?
> Making me wonder how the import mechanism interacts with the GIL, if it
> does?
> --
> https://mail.python.org/mailman/listinfo/python-list
>

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


Thread

How a module is being marked as imported? Jean-Charles Lefebvre <polyvertex@gmail.com> - 2016-02-04 02:19 -0800
  Re: How a module is being marked as imported? Jean-Charles Lefebvre <polyvertex@gmail.com> - 2016-02-04 07:35 -0800
    Re: How a module is being marked as imported? Kevin Conway <kevinjacobconway@gmail.com> - 2016-02-04 16:11 +0000

csiph-web