反复与间歇使用GIL

2024-09-26 22:09:04 发布

您现在位置:Python中文网/ 问答频道 /正文

是否对频繁获得/发行GIL与谨慎获得/发行GIL进行过比较?你知道吗

例如,以^{} 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_parrotbogart呢?现在想象一下这些微型GIL在C-library的其他地方都有。最好的办法是在你得到的任何机会释放GIL,还是越少越好?你知道吗


Tags: pytypeargsstaticactionpyobjectparrotstate

热门问题