-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[inference] Async dynamic batching (#4894)
* finish input and output logic * add generate * test forward * 1
- Loading branch information
Showing
4 changed files
with
139 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import asyncio | ||
|
||
shared_list = [] | ||
|
||
async def producer(): | ||
for i in range(5): | ||
await asyncio.sleep(1) # 模拟异步获取数据的操作 | ||
shared_list.append(i) | ||
print(f"Produced {i}") | ||
|
||
async def consumer(): | ||
last_index = 0 | ||
while True: | ||
await asyncio.sleep(0.5) # 为了不使循环过于紧凑,增加了小的延迟 | ||
if last_index < len(shared_list): | ||
item = shared_list[last_index] | ||
print(f"Consumed {item}") | ||
yield item | ||
last_index += 1 | ||
|
||
async def main(): | ||
# 创建生产者和消费者任务 | ||
prod_task = asyncio.create_task(producer()) | ||
|
||
# 等待生产者任务完成 | ||
await prod_task | ||
|
||
async for data in consumer(): | ||
print(data) | ||
# 为了示例的目的,我们只等待一段时间,然后停止消费者 | ||
await asyncio.sleep(5) | ||
|
||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters