This issue tracker has been migrated to GitHub ,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2012年08月13日 08:04 by jjdominguezm, last changed 2022年04月11日 14:57 by admin. This issue is now closed.
| Messages (11) | |||
|---|---|---|---|
| msg168071 - (view) | Author: Juan Javier (jjdominguezm) | Date: 2012年08月13日 08:04 | |
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? |
|||
| msg168098 - (view) | Author: R. David Murray (r.david.murray) * (Python committer) | Date: 2012年08月13日 12:52 | |
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. |
|||
| msg168099 - (view) | Author: R. David Murray (r.david.murray) * (Python committer) | Date: 2012年08月13日 12:54 | |
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. |
|||
| msg168100 - (view) | Author: Antoine Pitrou (pitrou) * (Python committer) | Date: 2012年08月13日 13:00 | |
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". |
|||
| msg168178 - (view) | Author: Juan Javier (jjdominguezm) | Date: 2012年08月14日 06:55 | |
Ok, you are right, serialized is the right name. Also, passing the lock to the decorator will the correct option. |
|||
| msg168179 - (view) | Author: Juan Javier (jjdominguezm) | Date: 2012年08月14日 08:10 | |
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 |
|||
| msg168996 - (view) | Author: Charles-François Natali (neologix) * (Python committer) | Date: 2012年08月24日 10:43 | |
"synchronized" has the merit of reusing Java's denomination, which this decorator intends to mimic, see http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html Basically, synchronize works with the object/class (implicit) reentrant lock. |
|||
| msg201398 - (view) | Author: Juan Javier (jjdominguezm) | Date: 2013年10月26日 21:19 | |
It looks like this is not very interesting after all. |
|||
| msg201473 - (view) | Author: R. David Murray (r.david.murray) * (Python committer) | Date: 2013年10月27日 16:24 | |
Are you saying that because you've lost interest, or because you think we have? Sometimes things get lost here for a while, but picked up again later... |
|||
| msg201503 - (view) | Author: Juan Javier (jjdominguezm) | Date: 2013年10月27日 21:11 | |
David, I think this doesn't deserve to be part of the library since it is trivial to write and it is just a particular use case. Adding it as an example in the threading module's documentation might be a good idea, what do you think? |
|||
| msg201504 - (view) | Author: Antoine Pitrou (pitrou) * (Python committer) | Date: 2013年10月27日 21:13 | |
I have never done any Java, but @serialized doesn't look like an useful idiom to me, so I don't think it would be worth giving as an example in the docs. |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:57:34 | admin | set | github: 59839 |
| 2013年10月27日 21:13:26 | pitrou | set | nosy:
+ tim.peters messages: + msg201504 |
| 2013年10月27日 21:11:47 | jjdominguezm | set | messages: + msg201503 |
| 2013年10月27日 16:24:27 | r.david.murray | set | messages: + msg201473 |
| 2013年10月26日 21:19:33 | jjdominguezm | set | status: open -> closed messages: + msg201398 |
| 2012年08月24日 10:43:33 | neologix | set | nosy:
+ neologix messages: + msg168996 |
| 2012年08月17日 18:13:33 | terry.reedy | set | title: synchronized decorator for the threading module -> Add serialized decorator to the threading module |
| 2012年08月14日 08:10:19 | jjdominguezm | set | messages: + msg168179 |
| 2012年08月14日 06:55:11 | jjdominguezm | set | messages: + msg168178 |
| 2012年08月13日 13:00:35 | pitrou | set | nosy:
+ jyasskin, pitrou messages: + msg168100 |
| 2012年08月13日 12:54:21 | r.david.murray | set | messages: + msg168099 |
| 2012年08月13日 12:52:09 | r.david.murray | set | nosy:
+ r.david.murray messages: + msg168098 |
| 2012年08月13日 08:04:28 | jjdominguezm | create | |