来自Python的C信号量

2024-09-30 20:33:23 发布

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

我有一个C代码,其中我使用信号量实现了一个锁机制。基本流程如下-

int s=1;
void wait(){

    while(s<=0);
    s--;
}

void pipeData(paTestData *data){

    wait();

    SAMPLE *tempBuff = (SAMPLE*)malloc(sizeof(SAMPLE)*FRAME_SIZE);
    int readCount   = FRAME_SIZE;

    while(readCount > 0){

        tempBuff[FRAME_SIZE - readCount] = data->sampleValues[data->readFromCB];        
        data->readFromCB++;
        readCount--;
    }

    fd = open(fifoPipe, O_WRONLY);

    write(fd, tempBuff, sizeof(tempBuff));
    close(fd);

    free(tempBuff);

}

int callBack(){

    // Perform data acquisition and call foo

   foo();

    // Rest code here
}

阅读器端的python代码如下:

^{pr2}$

PIPE另一端的数据正在由Python脚本处理。问题是python端的管道读取不能获取完整的数据集。在

这是因为我读过,对于named PIPE的每一次写入,都应该有一个读卡器,否则在下一次迭代中打开的管道将被阻塞。在

在这种情况下,在将10 samples写入管道之后,python的读取器实现能够read only first two samples,并且管道可以随时用于下一个写入集。在

这就是为什么我要找一个相同的locking mechanism。在

我的怀疑是-

1) Is there a way in which I could increment the s variable (kind of what a signal() function in C would do) every time the python script is finished polling up all data from PIPE.

2) Is there any other smooth implementation of such a problem, any other IPC technique between C and Python enabling lock mechanism?

谢谢!在


Tags: sample代码datasize管道frameintwait
1条回答
网友
1楼 · 发布于 2024-09-30 20:33:23
write(fd, tempBuff, sizeof(tempBuff));

上面的行只将4/8字节(32位/64位)的数据从tempBuff写入管道

如果要在tempBuff中写入所有数据,则需要将行更改为

^{pr2}$

相关问题 更多 >