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_ne w_thread(thread _extract_archiv e, ())
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. 1 3706
"Luxore" <al**********@g mail.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_ne w_thread(thread _extract_archiv e, ()) 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> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 late
July. A number of obscure crash-causing bugs have been fixed, various
memory leaks have been squished, but no new features have been added to
the language or to the library.
For more information on Python 2.3.1, including download links for...
|
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 the socket for the connection.
After I started the server, I can see one running process under Linux,
which is ok. But once I connected to the Server, I always see two
processes where one is the parent of the other. I think this should be
|
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 a separate thread.
Here's a little snippet of sample code that fails for me:
----
import thread, threading, wmi
def wmiTest(c, callee):
|
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 range(5): # start five threads passing i to each one
thread.start_new_thread(thrd,(i,))
|
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?
| |
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 create the objects (which is
easely fixed by calling Invoke()). Also, is there a way to acces the thread
which created the thread, or in other words: is there a parent/child
relationship between threads?
|
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?
===========================
import os
# import rest in a separate thread
def importRest():
import audio
|
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
KeyboardInterrupt
Unhandled exception in thread started by
Error in sys.excepthook:
|
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 (port_q,kbd_q))
print 'starting main loop'
error = Mainloop(s,port_q,active_q_list)
<end>
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |