473,379 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

thread variable scope with start_new_thread

Hello,

I am trying to create threaded python project and I'm running into some
weird Python variable scoping.

I am using the "thread" module (I know, it's old and I should be using
threading)... but for example:

<code>
import thread
def extract_archive(session, user, archive, dest=None):
job_id = sunlib.job.new(...)
def thread_extract_archive():
if not os.path.exists(archive):
<...do stuff...>
if not os.path.isfile(archive):
<...do stuff...>
if dest == None:
dest = os.path.dirname(archive)
<...do more stuff...>
thread.start_new_thread(thread_extract_archive, ())
return job_id
</code>

It appears that thread_extract_archive() inherits many of the variables
that are passed or defined in extract_archive(). I find this scary,
although very convenient because my thread process needs to reference a
number of items. The problem popped up when I added the "if dest ==
None" bit. Python barks that "dest" is an uninitialized variable.
That would make sense to me, but what I find odd is that
thread_extract_archive() does not have any trouble accessing the
session, user, or archive variables. The only difference is that I
pass these variables to other functions (e.g. os.path.isfile), and I am
doing a simple pythonic test on "dest."

What scares me is that my thread has access to extract_archive()
variables, but only if I access them in a certain way.

Any thoughts? Is there a way to use the more improved "threading"
module and pass my thread the entire variable scope of the parent
process like I am able to do above?

Thanks,

Luxore.

Nov 10 '05 #1
1 3688
"Luxore" <al**********@gmail.com> wrote:
I am trying to create threaded python project and I'm running into some
weird Python variable scoping.
the "weird scoping" you're seeing has nothing to do with threads
(read on)
I am using the "thread" module (I know, it's old and I should be using
threading)... but for example:

<code>
import thread
def extract_archive(session, user, archive, dest=None):
job_id = sunlib.job.new(...)
def thread_extract_archive():
if not os.path.exists(archive):
<...do stuff...>
if not os.path.isfile(archive):
<...do stuff...>
if dest == None:
dest = os.path.dirname(archive)
<...do more stuff...>
thread.start_new_thread(thread_extract_archive, ())
return job_id
</code>

It appears that thread_extract_archive() inherits many of the variables
that are passed or defined in extract_archive(). I find this scary,
although very convenient because my thread process needs to reference a
number of items. The problem popped up when I added the "if dest ==
None" bit. Python barks that "dest" is an uninitialized variable.
That would make sense to me, but what I find odd is that
thread_extract_archive() does not have any trouble accessing the
session, user, or archive variables. The only difference is that I
pass these variables to other functions (e.g. os.path.isfile), and I am
doing a simple pythonic test on "dest."


no, the difference is that you're assigning to "dest" in the inner function.
anything you assign to is considered to be a local variable:

http://docs.python.org/ref/naming.html

If a name is bound in a block, it is a local variable of that block.
If a name is bound at the module level, it is a global variable. /.../
If a variable is used in a code block but not defined there, it is a
free variable.

for more on nested scopes, read the above page carefully, and, if
you want even more details, this page:

http://www.python.org/peps/pep-0227.html

</F>

Nov 11 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: Anthony Baxter | last post by:
On behalf of the Python development team and the Python community, I'm happy to announce the release of Python 2.3.1 (final). Python 2.3.1 is a pure bug fix release of Python 2.3, released in...
3
by: Thomas Schmid | last post by:
Hi there, I wrote a tcp server which listens on a port. When he gets a new connection, he starts a new thread like this: thread.start_new_thread(self.ConnectionHandler, (conn,)) where conn is...
0
by: Fazan | last post by:
I'm using Tim Golden's WMI implementation (http://tgolden.sc.sabren.com/python/wmi.html) version 0.6 along with pywin32 build 202. I'm able to successfully query WMI in the main thread but not in...
15
by: Valkyrie | last post by:
Refering to the following codes I found, there is nothing displayed in the console, may I ask why? def thrd(param): # the thread worker function print "Received",param import thread for i in...
3
by: Konstantin Veretennicov | last post by:
Hi, Just curious: >>> import thread >>> help(thread.start_new_thread) . . . start_new_thread(function, args) . . . Second argument is mandatory. Is it incidental or for a reason?
7
by: MariusI | last post by:
Are objects implicitly stored in the TLS of the currently running thread? When creating multithreaded applications i get errors when accessing data from a different thread than the thread used to...
7
by: cyberco | last post by:
I want to import a long list of modules in a separate thread to speed things up. How can I make the modules imported in that separate thread accessible outside the method? ...
3
by: Salvatore Di Fazio | last post by:
Hi guys, when I close the application I get the following error: ----------------------------- Traceback (most recent call last): File "main.py", line 88, in <module> while exit : pass...
16
by: Hendrik van Rooyen | last post by:
I thought I would share this nasty little gotcha with the group. Consider the following code fragment: <start> print 'starting kbd thread' keyboard_thread = thread.start_new_thread(kbd_driver...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.