zoukankan      html  css  js  c++  java
  • Python 信号机制

    import signal

    def signalHandler(num,frame):
    pass
    signal.signal(signal.SIGTERM,signalHandler)

    http://guozhiwei.iteye.com/blog/939008
    http://www.sugarsfree.org/?p=179

    信号触发: signal_handler-->Py_AddPendingCall-->Py_MakePendingCalls-->checksignals_witharg-->PyErr_CheckSignals

    static PyObject *
    signal_signal(PyObject *self, PyObject *args)
    {
    PyObject *obj;
    int sig_num;
    PyObject *old_handler;
    void (*func)(int);
    if (!PyArg_ParseTuple(args, "iO:signal", &sig_num, &obj))
    return NULL;
    #ifdef WITH_THREAD
    if (PyThread_get_thread_ident() != main_thread) {
    PyErr_SetString(PyExc_ValueError,
    "signal only works in main thread");
    return NULL;
    }
    #endif
    if (sig_num < 1 || sig_num >= NSIG) {
    PyErr_SetString(PyExc_ValueError,
    "signal number out of range");
    return NULL;
    }
    if (obj == IgnoreHandler)
    func = SIG_IGN;
    else if (obj == DefaultHandler)
    func = SIG_DFL;
    else if (!PyCallable_Check(obj)) {
    PyErr_SetString(PyExc_TypeError,
    "signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object");
    return NULL;
    }
    else
    func = signal_handler; ——>注意signal_handler
    if (PyOS_setsig(sig_num, func) == SIG_ERR) {
    PyErr_SetFromErrno(PyExc_RuntimeError);
    return NULL;
    }
    old_handler = Handlers[sig_num].func;
    Handlers[sig_num].tripped = 0;
    Py_INCREF(obj);
    Handlers[sig_num].func = obj;
    return old_handler;
    }


    static void
    signal_handler(int sig_num)
    {
    #ifdef WITH_THREAD
    #ifdef WITH_PTH
    if (PyThread_get_thread_ident() != main_thread) {
    pth_raise(*(pth_t *) main_thread, sig_num);
    return;
    }
    #endif
    /* See NOTES section above */
    if (getpid() == main_pid) {
    #endif
    Handlers[sig_num].tripped = 1;
    /* Set is_tripped after setting .tripped, as it gets
    cleared in PyErr_CheckSignals() before .tripped.
    */
    is_tripped = 1;
    Py_AddPendingCall(checksignals_witharg, NULL); ——>关注Py_AddPendingCall
    #ifdef WITH_THREAD
    }
    #endif
    #ifdef SIGCHLD
    if (sig_num == SIGCHLD) {
    /* To avoid infinite recursion, this signal remains
    reset until explicit re-instated.
    Don't clear the 'func' field as it is our pointer
    to the Python handler...
    */
    return;
    }
    #endif
    PyOS_setsig(sig_num, signal_handler);
    }

    位于Modules/signalmodule.c
    http://magicode.me/index.php/2011/12/python-%E4%BF%A1%E5%8F%B7%E6%9C%BA%E5%88%B6%E6%B7%B1%E5%85%A5%E5%AD%A6%E4%B9%A0/
    http://blog.csdn.net/piaomiao245285/article/details/6278404

  • 相关阅读:
    浏览器网络相关概念
    量化投资:以python为工具
    Quantitative Strategies for Achieving Alpha (三)
    Quantitative Startegies for Achieving Alpha(二)
    Quantitative Strategies for Achieving Alpha(一)
    打开量化投资的黑箱(二)
    打开量化投资的黑箱(一)
    量化投资学习(一)
    handy源码阅读(六):tcp类
    handy源码阅读(六):udp类
  • 原文地址:https://www.cnblogs.com/moonflow/p/2413994.html
Copyright © 2011-2022 走看看