concurrency.go 590 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package python
  2. // See:
  3. // https://docs.python.org/3/c-api/init.html
  4. /*
  5. #define PY_SSIZE_T_CLEAN
  6. #include <Python.h>
  7. */
  8. import "C"
  9. //
  10. // ThreadState
  11. //
  12. type ThreadState struct {
  13. State *C.PyThreadState
  14. }
  15. func SaveThreadState() *ThreadState {
  16. return &ThreadState{C.PyEval_SaveThread()}
  17. }
  18. func (self *ThreadState) Restore() {
  19. C.PyEval_RestoreThread(self.State)
  20. }
  21. //
  22. // GilState
  23. //
  24. type GilState struct {
  25. State C.PyGILState_STATE
  26. }
  27. func EnsureGilState() *GilState {
  28. return &GilState{C.PyGILState_Ensure()}
  29. }
  30. func (self *GilState) Release() {
  31. C.PyGILState_Release(self.State)
  32. }