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

This project is not thread safe #9

Open
agronick opened this issue Apr 15, 2017 · 2 comments
Open

This project is not thread safe #9

agronick opened this issue Apr 15, 2017 · 2 comments

Comments

@agronick
Copy link

The function_queue variable is shared between all the threads on a process. If thread 1 adds something to the queue and then does some more processing and thread 2 comes along and finishes, even if it didn't add anything to the queue, it will process thread 1's function while thread 1 is still running.

You should replace function queue with a method that will pull the list from the threading.local and create it if it does not exist. Do not just put the variable in threading.local and leave it as a global variable. Then you'll just be sharing the thread local with all the other processes.

It should be something like this:

import threading
threadlocal = threading.local()    

def function_queue():
  queue = getattr(threadlocal, 'function_queue', None)
  if v is None:
    queue = []
    setattr(threadlocal, 'function_queue', queue)
  return queue
@kregoslup
Copy link

@agronick
Copy link
Author

agronick commented Jan 3, 2018

That might give you a speed increase. I think most people will only add a few functions so you'll see hardly any speed increase vs a list. It won't give you thread safety. You can use thread locals or you could add the fuctions to a property on the request object. That way even if people add functions from mutiple threads they'll be accociated with the request. I think hardly anyone would set callbacks from multiple threads though and you would have to start passing the request around to do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants