Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Threading misbehavior with lambdas #42528

Closed
fijal opened this issue Oct 27, 2005 · 2 comments
Closed

Threading misbehavior with lambdas #42528

fijal opened this issue Oct 27, 2005 · 2 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@fijal
Copy link

fijal commented Oct 27, 2005

BPO 1339045
Nosy @josiahcarlson

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2005-10-27.19:32:48.000>
created_at = <Date 2005-10-27.00:21:38.000>
labels = ['interpreter-core']
title = 'Threading misbehavior with lambdas'
updated_at = <Date 2005-10-27.19:32:48.000>
user = 'https://bugs.python.org/fijal'

bugs.python.org fields:

activity = <Date 2005-10-27.19:32:48.000>
actor = 'josiahcarlson'
assignee = 'none'
closed = True
closed_date = None
closer = None
components = ['Interpreter Core']
creation = <Date 2005-10-27.00:21:38.000>
creator = 'fijal'
dependencies = []
files = []
hgrepos = []
issue_num = 1339045
keywords = []
message_count = 2.0
messages = ['26746', '26747']
nosy_count = 2.0
nosy_names = ['josiahcarlson', 'fijal']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue1339045'
versions = ['Python 2.4']

@fijal
Copy link
Author

fijal commented Oct 27, 2005

suppose i write:
def f(x):
print x()

for i in range(3):
  f ( lambda : i )

I got 0,1,2

But when I write

for i in range(3):
  thread . start_new_thread ( f , ( lambda : i ) )

I got 2,2,2

Probably I don't get well design principles, but isn't
it against thread consistency? (as long as threads does
not interact with each other, interlace doesn't matter).

@fijal fijal closed this as completed Oct 27, 2005
@fijal fijal added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Oct 27, 2005
@fijal fijal closed this as completed Oct 27, 2005
@fijal fijal added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Oct 27, 2005
@josiahcarlson
Copy link
Mannequin

josiahcarlson mannequin commented Oct 27, 2005

Logged In: YES
user_id=341410

This is a bug in your understanding of lambdas, not in how
threads work. More specifically, lambdas do late binding.
By the time the threads have actually started executing and
call the lambda, the name 'i' is bound to the value 2.

If you need early binding, then you should bind early:

for i in xrange(3):
    thread.start_new_thread(f, (lambda i=i:i))

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)
Projects
None yet
Development

No branches or pull requests

1 participant