V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
linxy19957
V2EX  ›  Python

ctypes 库里的 create_string_buffer 缓冲区能被截断吗?

  •  
  •   linxy19957 · 2017-11-06 19:32:11 +08:00 via Android · 2961 次点击
    这是一个创建于 2363 天前的主题,其中的信息可能已经有所发展或是发生改变。
    问题是这样的,我调用 create_string_buffer 创建了一个 100mb 大小的缓冲区,然后调用了某个 dll 的函数修改了这个 buffer,dll 给我返回了修改的长度。
    我想调用 socket 的 sendall 函数把被修改的那部分发送出去,sendall(buf[:len])可以满足我的需求,但是 python 内部似乎是另外创建了一个字符串对象然后进行了拷贝,在数据量大的时候 cpu 占用有点高。
    请问有没有办法能对 buffer 进行一个截断,在实际不影响 buffer 大小的情况下,让 sendall 函数只发送部分数据呢?
    2 条回复    2017-11-06 23:19:26 +08:00
    justou
        1
    justou  
       2017-11-06 19:46:28 +08:00
    试试 memoryview
    NoAnyLove
        2
    NoAnyLove  
       2017-11-06 23:19:26 +08:00
    Python 中使用 slice 都会创建一个新的拷贝,memory slice 不会创建底层映射的拷贝
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3097 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 00:16 · PVG 08:16 · LAX 17:16 · JFK 20:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.