2022.7.25-内置函数close、len、cap解析

close

// The close built-in function closes a channel, which must be either
// bidirectional or send-only. It should be executed only by the sender,
// never the receiver, and has the effect of shutting down the channel after
// the last sent value is received. After the last value has been received
// from a closed channel c, any receive from c will succeed without
// blocking, returning the zero value for the channel element. The form
// x, ok := <-c
// will also set ok to false for a closed channel.
func close(c chan<- Type)

解析:内置函数close关闭一个信道,这个信道必须是双向的或仅发送的。它应该由发送端执行,而不是接收端。从一个已经关闭的信道里接收到最后一个值后,接收操作不会阻塞而是收到对应信道类型的零值。对一个已经被关闭没有数据的信道,x, ok := <-c形式也将收到ok为false。

下列情况会出错:

  1. 关闭仅能发送的信道
var ch = make(<-chan struct{}, 1)
close(ch)
  1. 发送到或关闭一个已经关闭的信道
var ch = make(chan struct{}, 1)
close(ch)
ch <- struct{}{}

//panic: send on closed channel
var ch = make(chan struct{}, 1)
close(ch)
close(ch)

//panic: close of closed channel

3 . 关闭nil信道

var ch chan struct{}
close(ch)

//panic: close of nil channel

len

// The len built-in function returns the length of v, according to its type:
// Array: the number of elements in v.
// Pointer to array: the number of elements in *v (even if v is nil).
// Slice, or map: the number of elements in v; if v is nil, len(v) is zero.
// String: the number of bytes in v.
// Channel: the number of elements queued (unread) in the channel buffer;
// if v is nil, len(v) is zero.
// For some arguments, such as a string literal or a simple array expression, the
// result can be a constant. See the Go language specification’s “Length and
// capacity” section for details.
func len(v Type) int

解析为:内置函数len根据v的类型,返回v的长度:

v的类型结果备注
字符串类型按字节表示的字符串长度
[n]T, *[n]T数组长度(== n)即使v为nil,也返回n
[]T分片长度如果v是nil,则返回0
map[K]T映射长度(定义的键的个数)如果v是nil,则返回0
chan T在信道缓冲区内排队的元素个数如果v是nil,则返回0

对于一些参数,例如字符串文本或简单的数组表达式,结果依然是一个常量。这种情况下,v是不求值的。

const AnyCell = 1

var y [][19]string
println(len(y[AnyCell]))

cap

// The cap built-in function returns the capacity of v, according to its type:
// Array: the number of elements in v (same as len(v)).
// Pointer to array: the number of elements in *v (same as len(v)).
// Slice: the maximum length the slice can reach when resliced;
// if v is nil, cap(v) is zero.
// Channel: the channel buffer capacity, in units of elements;
// if v is nil, cap(v) is zero.
// For some arguments, such as a simple array expression, the result can be a
// constant. See the Go language specification’s “Length and capacity” section for
// details.
func cap(v Type) int

解析:内置函数cap根据v的类型,返回v的容量。

v的类型结果备注
[n]T, *[n]T数组长度(== n)即使v为nil,也返回n
[]T分配容量如果v是nil,则返回0
chan T信道缓冲区容量如果v是nil,则返回0

对于一些参数,如如简单的数组表达式,结果也是一个常量。跟内置函数len相同。

分片的容量是为其底层数组所分配的空间所对应的元素个数。任何时间都满足如下关系:
0 <= len(s) <= cap(s)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以将动态调整画面分辨率的代码移动到接收视频帧之前,并将其放在每次循环之前,以便可以实时调整视频分辨率。以下是修改后的示例代码: ``` def run(self): self.sock.bind(self.ADDR) self.sock.listen() print('视频服务器已经启动...') print(f'\n工作地址是:{self.ADDR}') conn, addr = self.sock.accept() print(f'\n接受了远程视频客户端{addr}的连接...') data = "".encode("utf-8") payload_size = struct.calcsize("L") cv2.namedWindow('Remote',cv2.WINDOW_AUTOSIZE) while True: # 动态调整画面分辨率 cap = cv2.VideoCapture(0) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) if width > 800: scale_percent = 50 # 缩小画面尺寸 new_width = int(width * scale_percent / 100) new_height = int(height * scale_percent / 100) else: new_width = width new_height = height ret, frame = cap.read() if not ret: break frame = cv2.resize(frame, (new_width, new_height), interpolation=cv2.INTER_AREA) while len(data) < payload_size: data += conn.recv(81920) packed_size = data[:payload_size] data = data[payload_size:] msg_size = struct.unpack("L", packed_size)[0] while len(data) < msg_size: data += conn.recv(81920) zframe_data = data[:msg_size] data = data[msg_size:] frame_data = zlib.decompress(zframe_data) #解压数据 frame = pickle.loads(frame_data) #还原 cv2.imshow('Remote',frame) if cv2.waitKey(1) & 0xFF == 27: break cap.release() cv2.destroyAllWindows() conn.close() ``` 在每次循环开始之前,您可以使用OpenCV的VideoCapture对象获取当前视频帧的分辨率,并根据需要进行调整。然后,您可以使用cv2.resize函数将帧调整为新分辨率。最后,您可以通过调整后的帧继续进行后续处理和发送。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值