# 我的应用运行缓慢

## 在紧循环中使用 tryGet() 导致高 CPU 使用率

### 问题

如果你运行：

```python
while True:
    queue.tryGet()
```

tryGet() 是非阻塞的——无论帧是否就绪，它都会立即返回。 在紧循环中，这意味着你的程序尽可能快地旋转，占满一个 CPU 核心，并让其他线程或进程处于饥饿状态。

### 修复方法

a) 添加一个小延迟

让 CPU 在调用之间喘息：

```python
#!/usr/bin/env python3
import depthai as dai
import time
with dai.Pipeline() as pipeline:
    cam = pipeline.create(dai.node.Camera).build(
        dai.CameraBoardSocket.CAM_A, sensorFps=19.0
    )
    rawQueue = cam.raw.createOutputQueue()
    pipeline.start()
    while pipeline.isRunning():
        rawFrame = rawQueue.tryGet()
        if rawFrame is not None:
            print("Got a raw frame")
        time.sleep(0.001)  # 防止 CPU 使用率达到 100%
```

b) 使用 get() 代替 tryGet()

get() 会阻塞，直到帧就绪，因此没有忙循环：

```python
#!/usr/bin/env python3
import depthai as dai
import time
with dai.Pipeline() as pipeline:
    cam = pipeline.create(dai.node.Camera).build(
        dai.CameraBoardSocket.CAM_A, sensorFps=19.0
    )
    rawQueue = cam.raw.createOutputQueue()
    pipeline.start()
    while pipeline.isRunning():
        rawFrame = rawQueue.get()
        print("Got a raw frame")
```
