[issue15634] synchronized decorator for the threading module
Juan Javier added the comment: Ok, you are right, serialized is the right name. Also, passing the lock to the decorator will the correct option. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue15634 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue15634] synchronized decorator for the threading module
Juan Javier added the comment: What about this? def serialized(lock): def _serialized(func): def __serialized(*args, **kwds): with lock: return func(*args, **kwds) __serialized.__doc__ = func.__doc__ return __serialized return _serialized -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue15634 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue15634] synchronized decorator for the threading module
New submission from Juan Javier: I think it will be useful to have a decorator like this one on the threading module: def synchronized(func): A decorator to make a function execution synchronized. Examples: @synchronized def foo(): pass class Foo: def __init__(self): self.__syncdata = None @property def syncdata(self): return self.__syncdata @syncdata.setter @synchronized def syncdata(self, value): self.__syncdata = value if not hasattr(func, __lock): func.__lock = threading.Lock() def _synchronized(*args, **kwds): with func.__lock: func(*args, **kwds) _synchronized.__doc__ = func.__doc__ return _synchronized What do you think? -- components: Library (Lib) messages: 168071 nosy: jjdominguezm priority: normal severity: normal status: open title: synchronized decorator for the threading module type: enhancement versions: Python 3.4 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue15634 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue15634] synchronized decorator for the threading module
R. David Murray added the comment: Writing such a decorator is pretty trivial to do. On the other hand, I've done it often enough that I could be convinced it is useful to add. I think it would be better to have a decorator generator that takes a lock as its argument, however, since an application might well want to use the same lock for sections that it doesn't make sense to decorate, or use an RLock instead of a lock. If no lock is passed, a default Lock could be created. -- nosy: +r.david.murray ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue15634 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue15634] synchronized decorator for the threading module
R. David Murray added the comment: Oh, I misread your code. The code I'm working on uses the lock to serialize several different functions, and your decorator wouldn't work for that. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue15634 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue15634] synchronized decorator for the threading module
Antoine Pitrou added the comment: I'm not sure how useful that is in practice. Often you want to use the same lock accross several functions or methods. Also, I think it would be more accurate to call this serialized than synchronized. -- nosy: +jyasskin, pitrou ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue15634 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com