Created
October 22, 2010 18:50
-
-
Save felipecruz/641146 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static PyObject * | |
pyaio_write(PyObject *dummy, PyObject *args) { | |
const char *filename; | |
const char *buffer; | |
struct aiocb cb; | |
int ret, offset, numbytes; | |
Pyaio_cb *aio; | |
PyObject *result = NULL; | |
PyObject *callback; | |
FILE *file = NULL; | |
struct sigaction sa; | |
if (PyArg_ParseTuple(args, "ssiiO:set_callback", &filename, &buffer, &offset, &numbytes, &callback)) { | |
if (!PyCallable_Check(callback)) { | |
PyErr_SetString(PyExc_TypeError, | |
"parameter must be callable"); | |
return NULL; | |
} | |
Py_XINCREF(callback); /* Add a reference to new callback */ | |
} | |
file = fopen(filename, "w"); | |
aio = malloc(sizeof(Pyaio_cb)); | |
aio->cb = malloc(sizeof(struct aiocb)); | |
bzero((char *) aio->cb, sizeof(struct aiocb)); | |
aio->callback = callback; | |
sa.sa_flags = SA_RESTART; | |
sigemptyset(&sa.sa_mask); | |
sa.sa_sigaction = aio_write_completion_handler; | |
sa.sa_flags = SA_SIGINFO; | |
sigaction(SIGUSR1, &sa, NULL); | |
aio->cb->aio_buf = malloc((numbytes+1) * sizeof(char)); | |
printf("%s\n", buffer); | |
printf("%d\n", numbytes); | |
printf("%s\n", aio->cb->aio_buf); | |
strncpy(aio->cb->aio_buf, "AAAAAAAAA", numbytes); | |
//aio->cb->aio_buf[numbytes] = '\0'; | |
printf("OK1"); | |
aio->cb->aio_fildes = fileno(file); | |
aio->cb->aio_nbytes = numbytes; | |
aio->cb->aio_offset = offset; | |
aio->cb->aio_sigevent.sigev_notify = SIGEV_SIGNAL; | |
aio->cb->aio_reqprio = 0; | |
aio->cb->aio_sigevent.sigev_signo = SIGUSR1; | |
aio->cb->aio_sigevent.sigev_notify_attributes = NULL; | |
aio->cb->aio_sigevent.sigev_value.sival_ptr = aio; | |
ret = aio_write(aio->cb); | |
printf("write return %d", ret); | |
if (ret < 0) | |
perror("aio_write"); | |
return Py_BuildValue("i", ret); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment