473,396 Members | 1,966 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,396 software developers and data experts.

multithreading-problem

Hi,

I expirienced strange problems when using the module threading:

class Bar:
def __init__(_, arg):
_.arg = arg
def work(_):
while 1:
print _.arg

def foo(arg):
b = Bar(arg)
b.work()

for i in xrange(4):
thread = threading.Thread(target=lambda : foo(i))
thread.start()

What I'd expect would be a sequence of prints like this
1
2
3
4
1
3
4
2

What I actualy get is this:
1
4
4
4
4
1
4
4
4

Placing a time.sleep(3) after the thread.start() fixed things. So it appears
that the curried lambda passed as target is somehow a reference equal for
all four invocations.

The used python is 2.2.<latest-cygwin>.

Regards,

Diez
Jul 18 '05 #1
4 2158
Diez B. Roggisch wrote:
Hi,

I expirienced strange problems when using the module threading:

class Bar:
def __init__(_, arg):
_.arg = arg
def work(_):
while 1:
print _.arg

def foo(arg):
b = Bar(arg)
b.work()

for i in xrange(4):
thread = threading.Thread(target=lambda : foo(i))
thread.start()

What I'd expect would be a sequence of prints like this
1
2
3
4
1
3
4
2

What I actualy get is this:
1
4
4
4
4
1
4
4
4

Placing a time.sleep(3) after the thread.start() fixed things. So it appears
that the curried lambda passed as target is somehow a reference equal for
all four invocations.


No, I don't believe it to be true. It's probably just that the
other threads aren't getting a chance to run. Consider
time.sleep(0) in the while loop in work()

-Manish

--
Manish Jethani (manish.j at gmx.net)
phone (work) +91-80-51073488

Jul 18 '05 #2
Diez B. Roggisch wrote:
...
for i in xrange(4):
thread = threading.Thread(target=lambda : foo(i))
thread.start()
... So it appears that the curried lambda passed as target is
somehow a reference equal for all four invocations.... This is why the "curry" recipe dances more jig than you have.
the lambda-expression that you've passed in did not do any
value-capture. The smallest fix is: ...
for i in xrange(4):
thread = threading.Thread(target=lambda v=i: foo(v))
thread.start()
...
With curry from the Python Cookbook (or as below), I'd write: ...
for i in xrange(4):
thread = threading.Thread(target=curry(foo, i))
thread.start()
...


The problem is that your lambda expression looks up "i" at function
evaluation time (when target is called), not when you assign the
target function for the thread.

-Scott David Daniels
Sc***********@Acm.Org
=============================
for 2.2 I define curry as:

from __future__ import nested_scopes

def curry(*_args, **_kwargs):
"""curry(f,<args>)(<more>) == f(<args>, <more>) (roughly).

keyword args in the curry call can be overridden by keyword args
in the later call; curry can be used to change defaults.
"""
def result(*__args, **__kwargs):
if _kwargs and __kwargs:
kwargs = _kwargs.copy()
kwargs.update(__kwargs)
else:
kwargs = _kwargs or __kwargs
return _args[0](*(_args[1:]+__args), **kwargs)
return result

Jul 18 '05 #3
Manish Jethani wrote:

Placing a time.sleep(3) after the thread.start() fixed things. So it
appears that the curried lambda passed as target is somehow a reference
equal for all four invocations.


No, I don't believe it to be true. It's probably just that the
other threads aren't getting a chance to run. Consider
time.sleep(0) in the while loop in work()


No, they all run - just a few of them have the same parameters. In my real
app, all of them do some work, which takes an individual amount of time. In
the main-thread I wait for all of them to be terminated, and while doing
that print how much are still running. All this works as expected. The only
thing not working is the parameter passing.

Diez
Jul 18 '05 #4
> The problem is that your lambda expression looks up "i" at function
evaluation time (when target is called), not when you assign the
target function for the thread.


Ah, ok. That makes sense. Where do I find this curry-function, only in the
book? It appears not to be a standard function.

Regards, Diez
Jul 18 '05 #5

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

Similar topics

1
by: dixp | last post by:
I'm new to writing multithreaded apps and I have a design question. I have a winforms app and a class which has a method that does processing which is time intensive. I want the user to be able...
47
by: mihai | last post by:
What does the standard say about those two? Is any assurance that the use of STL is thread safe? Have a nice day, Mihai.
1
by: GianGuz | last post by:
In the following example Global is able to create and manage access to objects of any kind (even in a multithreading environment) with and index value attached to. So a Global<0, string> is a...
5
by: sarge | last post by:
I would like to know how to perform simple multithreading. I had created a simple form to test out if I was multithreading properly, but got buggy results. Sometime the whole thig would lock up...
9
by: tommy | last post by:
hi, i have found a example for multithreading and asp.net http://www.fawcette.com/vsm/2002_11/magazine/features/chester/ i want to speed up my website ... if my website is starting, they...
7
by: noid droid | last post by:
Greetings. I received 4 VB .NET books and looking through the indices and tables of contents, I see that none of them addresses multithreading in VB ..NET. I just bought a bunch of books because...
2
by: Rich | last post by:
Hello, I have set up a multithreading routine in a Test VB.net proj, and it appears to be working OK in debug mode and I am not using synchronization. Multithreading is a new thing for me, and...
55
by: Sam | last post by:
Hi, I have a serious issue using multithreading. A sample application showing my issue can be downloaded here: http://graphicsxp.free.fr/WindowsApplication11.zip The problem is that I need to...
5
by: sandy82 | last post by:
Whats actuallly multithreading is ... and how threading and multithreading differ . Can any1 guide how multithreading is used on the Web .. i mean a practical scenario in which u use...
7
by: Ray | last post by:
Hello, Greetings! I'm looking for a solid C++ multithreading book. Can you recommend one? I don't think I've seen a multithreading C++ book that everybody thinks is good (like Effective C++ or...
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...
0
marktang
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
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,...

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.