Ver código fonte

Release the GIL in accelerated functions.

Release the GIL so that we can do other stuff in Python threads, but be careful not to do expensive operations or it might slow down the accelerated functions (even though they run in separate OS threads).
Steven Engler 6 anos atrás
pai
commit
57a3b7d7d7
1 arquivos alterados com 14 adições e 2 exclusões
  1. 14 2
      src/accelerated_functions.c

+ 14 - 2
src/accelerated_functions.c

@@ -148,7 +148,13 @@ static PyObject *py_push_data(PyObject *self, PyObject *args){
 		return NULL;
 	}
 	//
-	int ret_val = push_data(socket, bytes_total, buffer, buffer_len);
+	int ret_val;
+	Py_BEGIN_ALLOW_THREADS
+	// GIL is unlocked, but don't do expensive operations in
+	// other threads or it might slow this one down
+	ret_val = push_data(socket, bytes_total, buffer, buffer_len);
+	Py_END_ALLOW_THREADS
+	//
 	PyObject* py_ret_val = PyLong_FromLong(ret_val);
 	//
 	return py_ret_val;
@@ -165,7 +171,13 @@ static PyObject *py_pull_data(PyObject *self, PyObject *args){
 	}
 	//
 	double elapsed_time = 0;
-	int ret_val = pull_data(socket, bytes_total, buffer_len, &elapsed_time);
+	int ret_val;
+	Py_BEGIN_ALLOW_THREADS
+	// GIL is unlocked, but don't do expensive operations in
+	// other threads or it might slow this one down
+	ret_val = pull_data(socket, bytes_total, buffer_len, &elapsed_time);
+	Py_END_ALLOW_THREADS
+	//
 	PyObject* py_ret_val = Py_BuildValue("(id)", ret_val, elapsed_time);
 	//
 	return py_ret_val;