example_ctypes.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:ringbuffer 作者: bslatkin 项目源码 文件源码
def writer(ring, start, count):
    for i in range(start, start + count):
        data = os.urandom(random.randint(1, 1000))
        time_micros = int(time.time() * 10**6)
        record = Record(
            write_number=i,
            timestamp_microseconds=time_micros,
            length=len(data))
        # Note: You can't pass 'data' to the constructor without doing an
        # additional copy to convert the bytes type to a c_ubyte * 1000. So
        # instead, the constructor will initialize the 'data' field's bytes
        # to zero, and then this assignment overwrites the data-sized part.
        record.data[:len(data)] = data

        try:
            ring.try_write(record)
        except ringbuffer.WaitingForReaderError:
            print('Reader is too slow, dropping %d' % i)
            continue

        if i and i % 100 == 0:
            print('Wrote %d so far' % i)

    ring.writer_done()
    print('Writer is done')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号