栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用sys.getsizeof(Var)方法的python大小的ctypes与`ctypes.sizeof(Var)`

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用sys.getsizeof(Var)方法的python大小的ctypes与`ctypes.sizeof(Var)`

如果您想了解详细信息,则应该查看一下

objects.h
(尤其是文件顶部的注释)。您
ctypes.c_byte(1)
是一个Python对象:

>>> import sys>>> import ctypes>>> isinstance(ctypes.c_byte(1), object)True

如@Daniel所述,

sys.getsizeof
获取该Python对象的大小。该Python对象大于C中相应的对象。请注意以下几点
object.h

Objects are structures allocated on the heap. . . .The actual memory allocated for an objectcontains other data that can only be accessed after casting the pointerto a pointer to a longer structure type.  This longer type must startwith the reference count and type fields; the macro PyObject_HEAD should beused for this.

换句话说,宏

PyObject_HEAD
附加到每个对象的开头。这增加了Python对象的大小。

ctypes.sizeof
另一方面,返回
C
Python对象中数据类型的实际大小(使用C的
sizeof
运算符)。

编辑

根据您在Daniel的评论中提到的目标,可以在Python
3.x中通过服务器发送一个字节。下面是一个示例,说明如何使用Python的

socket
模块发送字节来证明这一点。

这是服务器,您将在一个Python解释器中运行该服务器:

# Serverimport socketHOST = ''# All available interfacesPORT = 50007        # Same port as clients = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.bind((HOST, PORT))s.listen(1)conn, addr = s.accept()print('Connected by', addr)while True:    data = conn.recv(1)        # receive data with bufsize 1; a larger bufsize will break this pre    if not data: break    conn.sendall(data)conn.close()

这是客户端,您将在另一个python解释器中运行该客户端:

# Clientimport socketHOST = '127.0.0.1'  # The remote host, but here using localhostPORT = 50007        # The port used by both the client and servers = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.connect((HOST, PORT))s.sendall(b'1')     # a bytes objectdata = s.recv(1)    # Receive data from the socket with bufsize of 1s.close()print('Received', repr(data))  # confirm receipt


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/410808.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号