interesting threading result..

A

Abandoned

Hi..
I have a interesting threading result..

class GetData(threading.Thread):
def __init__(self, name):
threading.Thread.__init__(self)
self.name = name

def run(self):
self.data={2:3, 3:4...}

current = GetData(a)
nlist.append(current)
current.start()
end=[]
dd=nlist[0]
dd.join()
result=dd.data
print result
(and it gives me : {2:3, 3:4...} )
end.append(result)
print end
(and it gives me: [<GetData(Thread-3, stopped)>, {2:3, 3:4...}] )
What happen ??
What is the <GetData(Thread-3, stopped)>, ?
In the first print there isn't it but second print it is in there.
I don't want <GetData(Thread-3, stopped)>..
How can i escape from it ?

I'm sorry my bad english.
King regards..
 
A

Alan Franzoni

Il Mon, 29 Oct 2007 09:08:01 -0700, Abandoned ha scritto:
I'm sorry my bad english.
King regards..

You must have messed up with something. You didn't post a working snippet
in first place, so you should do it now. I suppose you're working with some
editor or IDE which mantains the very same instance of a python interpreter
in memory; you probably have done something like

end.append(current)

at a certain point in your tests.

I converted your pseudocode to python like that:

import threading

class GetData(threading.Thread):
def __init__(self, name):
threading.Thread.__init__(self)
self.name = name

def run(self):
self.data={2:3, 3:4}

nlist = []
current = GetData("a")
nlist.append(current)
current.start()
end=[]
dd=nlist[0]
dd.join()
result=dd.data
print result
end.append(result)
print end

and I just get the result I would expect:

{2: 3, 3: 4}
[{2: 3, 3: 4}]


--
Alan Franzoni <[email protected]>
-
Togli .xyz dalla mia email per contattarmi.
Remove .xyz from my address in order to contact me.
-
GPG Key Fingerprint (Key ID = FE068F3E):
5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E
 
A

Abandoned

Il Mon, 29 Oct 2007 09:08:01 -0700, Abandoned ha scritto:
I'm sorry my bad english.
King regards..

You must have messed up with something. You didn't post a working snippet
in first place, so you should do it now. I suppose you're working with some
editor or IDE which mantains the very same instance of a python interpreter
in memory; you probably have done something like

end.append(current)

at a certain point in your tests.

I converted your pseudocode to python like that:

import threading

class GetData(threading.Thread):
def __init__(self, name):
threading.Thread.__init__(self)
self.name = name

def run(self):
self.data={2:3, 3:4}

nlist = []
current = GetData("a")
nlist.append(current)
current.start()
end=[]
dd=nlist[0]
dd.join()
result=dd.data
print result
end.append(result)
print end

and I just get the result I would expect:

{2: 3, 3: 4}
[{2: 3, 3: 4}]

--
Alan Franzoni <[email protected]>
-
Togli .xyz dalla mia email per contattarmi.
Remove .xyz from my address in order to contact me.
-
GPG Key Fingerprint (Key ID = FE068F3E):
5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E

Thanks you..
Now this is my real code and the problem continue.
What is the mistake?

import threading


class GetData(threading.Thread):
def __init__(self, name):
threading.Thread.__init__(self)
self.name = name


def run(self):
self.data={2:3, 3:4}


i=0
datas={"n": [['msn']]}
nglist=nlist={0: list()}
for x in query:
tip=x
for a in datas[x]:
for o in a:
if len(o)>1:
current = GetData(o)
nglist.append(current)
current.start()

i=i+1

for x in range(0,len(nglist)):
for jk in range(0,len(nglist[x])):
jkl=nglist[x][jk]
jkl.join()
sonuc=jkl.data
nlist[x].append(sonuc)

print nlist
{0: [<GetData(Thread-1, stopped)>, {2: 3, 3: 4}]}
 
G

Gabriel Genellina

Now this is my real code and the problem continue.
What is the mistake?

I don't see where you define "query" so this can't be your complete code.
import threading


class GetData(threading.Thread):
def __init__(self, name):
threading.Thread.__init__(self)
self.name = name


def run(self):
self.data={2:3, 3:4}


i=0
datas={"n": [['msn']]}
nglist=nlist={0: list()}

Here, you make nglist and nlist refer to the same object (a dictionary,
not a list!)
To be short: nglist and nlist are the same thing
for x in query:
tip=x
for a in datas[x]:
for o in a:
if len(o)>1:
current = GetData(o)
nglist.append(current)
current.start()


Here, you append current to nglist. current is an instance of the
GetData class, a Thread. As nglist is the same thing as nlist, nlist
contains a GetData instance too.
i=i+1

for x in range(0,len(nglist)):
for jk in range(0,len(nglist[x])):
jkl=nglist[x][jk]
jkl.join()
sonuc=jkl.data
nlist[x].append(sonuc)

print nlist
{0: [<GetData(Thread-1, stopped)>, {2: 3, 3: 4}]}

It's not surprising to see here the object appended above.

I can't say how to improve/fix this because I can't see the purpose of all
this. Could you explain what do you want to do exactly?
 
D

Dennis Lee Bieber

Thanks you..
Now this is my real code and the problem continue.
What is the mistake?
It is NOT a runnable copy of your code -- where does "query" come
from?
i=0
datas={"n": [['msn']]}
nglist=nlist={0: list()}

Note that both nglist and nlist are names attached to the same
single DICTIONARY object. So changing (mutating) the dictionary with one
name will also be seen by code using the other name.

Unlike "classical" languages, variable name in Python do not
represent fixed memory addresses, and assignment does NOT copy the right
hand side /to/ the address of the left hand side -- instead assignment
changes the LHS to "point at" the RHS.

So... to create TWO dictionaries USE:

nglist = {0 : list()}
nlist = {0 : []} #alternate for an empty list
for x in query:

Let us assume query is the string "n"...

x is now "n" (any other value will fail when keying into the datas
dictionary)
tip=x
for a in datas[x]:

datas["n"] is the nested list [["msn"]]
for o in a:

o is the first element of a: ["msn"]
if len(o)>1:

o is a list of one element ( "msn" ), so len(o) == 1, this if
statement is false, and the following won't be executed.
current = GetData(o)
nglist.append(current)
current.start()

i=i+1

for x in range(0,len(nglist)):
for jk in range(0,len(nglist[x])):
jkl=nglist[x][jk]


Just use:

for jkl in nglist[x]:
jkl.join()
sonuc=jkl.data
nlist[x].append(sonuc)

You've just, since in your code nglist and nlist are the same
dictionary, appended "sonuc" to the list holding the thread object
itself.
print nlist
{0: [<GetData(Thread-1, stopped)>, {2: 3, 3: 4}]}
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top