是否经常与少量获取/释放GIL进行比较?
例如,选择keywdarg_parrot
function defined here。如果开发人员想要了解可能需要GIL的其他线程,他们可以将其重写为:
static PyObject *
keywdarg_parrot(PyObject *self, PyObject *args, PyObject *keywds)
{
int voltage;
char *state = "a stiff";
char *action = "voom";
char *type = "Norwegian Blue";
static char *kwlist[] = {"voltage", "state", "action", "type", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "i|sss", kwlist,
&voltage, &state, &action, &type))
return NULL;
Py_BEGIN_ALLOW_THREADS
printf("-- This parrot wouldn't %s if you put %i Volts through it.\n", action, voltage);
printf("-- Lovely plumage, the %s -- It's %s!\n", type, state);
Py_END_ALLOW_THREADS
Py_INCREF(Py_None);
return Py_None;
}
毕竟,GIL不需要 来使用printf
,那么为什么让keywdarg_parrot
使用它?现在想象一下这些微型GIL采集在C库中的其他地方。是否最好在任何&你得到的每一个机会,还是少即是多的情况?