Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

50 Zeilen
1.2 KiB

  1. import asyncio
  2. import functools
  3. def delegate_to_executor(*attrs):
  4. def cls_builder(cls):
  5. for attr_name in attrs:
  6. setattr(cls, attr_name, _make_delegate_method(attr_name))
  7. return cls
  8. return cls_builder
  9. def proxy_method_directly(*attrs):
  10. def cls_builder(cls):
  11. for attr_name in attrs:
  12. setattr(cls, attr_name, _make_proxy_method(attr_name))
  13. return cls
  14. return cls_builder
  15. def proxy_property_directly(*attrs):
  16. def cls_builder(cls):
  17. for attr_name in attrs:
  18. setattr(cls, attr_name, _make_proxy_property(attr_name))
  19. return cls
  20. return cls_builder
  21. def _make_delegate_method(attr_name):
  22. @asyncio.coroutine
  23. def method(self, *args, **kwargs):
  24. cb = functools.partial(getattr(self._file, attr_name),
  25. *args, **kwargs)
  26. return (yield from self._loop.run_in_executor(self._executor, cb))
  27. return method
  28. def _make_proxy_method(attr_name):
  29. def method(self, *args, **kwargs):
  30. return getattr(self._file, attr_name)(*args, **kwargs)
  31. return method
  32. def _make_proxy_property(attr_name):
  33. def proxy_property(self):
  34. return getattr(self._file, attr_name)
  35. return property(proxy_property)