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

add a threadsafe option #83

Open
mmerickel opened this issue Jul 26, 2024 · 0 comments
Open

add a threadsafe option #83

mmerickel opened this issue Jul 26, 2024 · 0 comments

Comments

@mmerickel
Copy link

mmerickel commented Jul 26, 2024

It seems lazy-object-proxy is not threadsafe. If you look at the following example you will see the doit invoked multiple times in some runs of the script. For my use-cases it needs a threadsafe version of this, as the object being created is at module-scope and threadsafe itself (re.compile). It's not a big deal in some scenarios that 2 copies are created but it's not great and quite subtle when moving to this lazy-creation versus immediate creation when refactoring to use this library. Thoughts?

from lazy_object_proxy import Proxy
import threading

x = 0

def doit():
    global x
    x += 1
    print('doing it', x)
    return x

p = Proxy(doit)

def work(obj):
    print(obj)

threading.Thread(None, target=work, args=(p,)).start()
threading.Thread(None, target=work, args=(p,)).start()

output:

loop 0
doing it 1
1
doing it 2 <-- doit invoked again for the same proxy
2

expected:

loop 0
doing 1
1
1
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

1 participant