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 and KeyError: 51 #52997

Closed
VigneshK mannequin opened this issue May 18, 2010 · 3 comments
Closed

Threading and KeyError: 51 #52997

VigneshK mannequin opened this issue May 18, 2010 · 3 comments

Comments

@VigneshK
Copy link
Mannequin

VigneshK mannequin commented May 18, 2010

BPO 8751

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 2010-05-18.15:45:31.961>
created_at = <Date 2010-05-18.14:56:47.684>
labels = ['expert-IDLE', 'invalid']
title = 'Threading and KeyError: 51'
updated_at = <Date 2010-05-18.15:45:31.950>
user = 'https://bugs.python.org/VigneshK'

bugs.python.org fields:

activity = <Date 2010-05-18.15:45:31.950>
actor = 'draghuram'
assignee = 'none'
closed = True
closed_date = <Date 2010-05-18.15:45:31.961>
closer = 'draghuram'
components = ['IDLE']
creation = <Date 2010-05-18.14:56:47.684>
creator = 'Vignesh.K'
dependencies = []
files = []
hgrepos = []
issue_num = 8751
keywords = []
message_count = 3.0
messages = ['105981', '105985', '105986']
nosy_count = 3.0
nosy_names = ['draghuram', 'Vignesh.K', 'Mattias.Nilsson']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue8751'
versions = ['Python 2.5']

@VigneshK
Copy link
Mannequin Author

VigneshK mannequin commented May 18, 2010

I have a python script which runs a particular script large number of times (for monte carlo purpose) and the way I have scripted it is that, I queue up the script the desired number of times it should be run then I spawn threads and each thread runs the script once and again when its done.

Once the script in a particular thread is finished, the output is written to a file by accessing a lock (so my guess was that only one thread accesses the lock at a given time). Once the lock is released by one thread, the next thread accesses it and adds its output to the previously written file and rewrites it.

I am not facing a problem when the number of iterations is small like 10 or 20 but when its large like 50 or 150, python returns a KeyError: 51 telling me element doesn't exist and the error it points out to is within the lock which puzzles me since only one thread should access the lock at once and I do not expect an error.

This is the class I use:

class errorclass(threading.Thread):
    
     def __init__(self, queue):
         self.__queue=queue
         threading.Thread.__init__(self)
     
     def run(self):
         while 1:
               item = self.__queue.get()
               if item is None: break
               result = myfunction()
               lock = threading.RLock()
               lock.acquire()
               ADD entries from current thread to entries in file and
               REWRITE FILE           
               lock.release()

@VigneshK VigneshK mannequin added the topic-IDLE label May 18, 2010
@MattiasNilsson
Copy link
Mannequin

MattiasNilsson mannequin commented May 18, 2010

Correct me if I'm wrong, but you seem to be creating a new lock object for each iteration in the loop?
If other threads should be blocked, they must be using the same lock object and you can't create new ones for each time you want to write something to the file.

This doesn't seem to be a Python bug at all, but instead a problem in your implementation. It might be better to post this at stackoverflow.com or somewhere similar.

@draghuram
Copy link
Mannequin

draghuram mannequin commented May 18, 2010

As suggested, please post in c.l.py or other forums. Open a bug here only if you are sure that there is a bug in Python.

@draghuram draghuram mannequin closed this as completed May 18, 2010
@draghuram draghuram mannequin added the invalid label May 18, 2010
@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
Projects
None yet
Development

No branches or pull requests

0 participants