Như đã đề cập bởi pajton, bạn có thể viết phần mở rộng C để gọi hệ thống gọi setitimer()
. Điều này không quá khó vì bạn có thể chỉ cần sao chép mã số signal.getitimer()
và signal.setitimer()
từ nguồn của các phiên bản sau của Python. Chúng chỉ là các trình bao bọc mỏng xung quanh các cuộc gọi hệ thống được đặt tên giống nhau.
Tùy chọn này chỉ khả thi nếu bạn đang sử dụng CPython và bạn đang ở trong một môi trường cho phép bạn sử dụng tiện ích mở rộng tùy chỉnh C.
Sửa: Đây là mã sao chép từ signalmodule.c
in Python 2.7 (giấy phép của Python áp dụng):
#include "Python.h"
#include <sys/time.h>
static PyObject *ItimerError;
/* auxiliary functions for setitimer/getitimer */
static void
timeval_from_double(double d, struct timeval *tv)
{
tv->tv_sec = floor(d);
tv->tv_usec = fmod(d, 1.0) * 1000000.0;
}
Py_LOCAL_INLINE(double)
double_from_timeval(struct timeval *tv)
{
return tv->tv_sec + (double)(tv->tv_usec/1000000.0);
}
static PyObject *
itimer_retval(struct itimerval *iv)
{
PyObject *r, *v;
r = PyTuple_New(2);
if (r == NULL)
return NULL;
if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_value)))) {
Py_DECREF(r);
return NULL;
}
PyTuple_SET_ITEM(r, 0, v);
if(!(v = PyFloat_FromDouble(double_from_timeval(&iv->it_interval)))) {
Py_DECREF(r);
return NULL;
}
PyTuple_SET_ITEM(r, 1, v);
return r;
}
static PyObject *
itimer_setitimer(PyObject *self, PyObject *args)
{
double first;
double interval = 0;
int which;
struct itimerval new, old;
if(!PyArg_ParseTuple(args, "id|d:setitimer", &which, &first, &interval))
return NULL;
timeval_from_double(first, &new.it_value);
timeval_from_double(interval, &new.it_interval);
/* Let OS check "which" value */
if (setitimer(which, &new, &old) != 0) {
PyErr_SetFromErrno(ItimerError);
return NULL;
}
return itimer_retval(&old);
}
PyDoc_STRVAR(setitimer_doc,
"setitimer(which, seconds[, interval])\n\
\n\
Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL\n\
or ITIMER_PROF) to fire after value seconds and after\n\
that every interval seconds.\n\
The itimer can be cleared by setting seconds to zero.\n\
\n\
Returns old values as a tuple: (delay, interval).");
static PyObject *
itimer_getitimer(PyObject *self, PyObject *args)
{
int which;
struct itimerval old;
if (!PyArg_ParseTuple(args, "i:getitimer", &which))
return NULL;
if (getitimer(which, &old) != 0) {
PyErr_SetFromErrno(ItimerError);
return NULL;
}
return itimer_retval(&old);
}
PyDoc_STRVAR(getitimer_doc,
"getitimer(which)\n\
\n\
Returns current value of given itimer.");
static PyMethodDef itimer_methods[] = {
{"setitimer", itimer_setitimer, METH_VARARGS, setitimer_doc},
{"getitimer", itimer_getitimer, METH_VARARGS, getitimer_doc},
{NULL, NULL} /* sentinel */
};
PyMODINIT_FUNC
inititimer(void)
{
PyObject *m, *d, *x;
int i;
m = Py_InitModule3("itimer", itimer_methods, 0);
if (m == NULL)
return;
d = PyModule_GetDict(m);
#ifdef ITIMER_REAL
x = PyLong_FromLong(ITIMER_REAL);
PyDict_SetItemString(d, "ITIMER_REAL", x);
Py_DECREF(x);
#endif
#ifdef ITIMER_VIRTUAL
x = PyLong_FromLong(ITIMER_VIRTUAL);
PyDict_SetItemString(d, "ITIMER_VIRTUAL", x);
Py_DECREF(x);
#endif
#ifdef ITIMER_PROF
x = PyLong_FromLong(ITIMER_PROF);
PyDict_SetItemString(d, "ITIMER_PROF", x);
Py_DECREF(x);
#endif
ItimerError = PyErr_NewException("itimer.ItimerError",
PyExc_IOError, NULL);
if (ItimerError != NULL)
PyDict_SetItemString(d, "ItimerError", ItimerError);
}
Lưu mã này như itimermodule.c
, biên dịch nó thành một phần mở rộng C sử dụng một cái gì đó giống như
gcc -I /usr/include/python2.4 -fPIC -o itimermodule.o -c itimermodule.c
gcc -shared -o itimer.so itimermodule.o -lpython2.4
Bây giờ, nếu bạn may mắn, bạn sẽ có thể nhập từ Python bằng cách sử dụng
import itimer
và gọi itimer.setitimer()
.
viết một phần mở rộng C là một lựa chọn ... – pajton
Để ai bình chọn để đóng câu hỏi này hỗ trợ các ý tưởng được một bản sao chính xác của [này ] (http://stackoverflow.com/q/3770711/146792): bạn có thực sự đọc câu hỏi OP cho đến khi kết thúc không? Câu hỏi là IMO hợp lệ hoàn toàn. – mac
@mac: Tôi nghĩ người đã bỏ phiếu để đóng đã tìm ra lỗi của cô ấy - ít nhất cô ấy đã xóa nhận xét được tạo tự động. :) –