.dump()
可以(显然)将输出写成几个块,如果它足够大的话。
你会想用
.dumps()
先编码成JSON字符串,然后
.write()
strace
在码头集装箱里,看看能得到什么
write()
d。
(host) $ docker run -it ubuntu:18.04 bash
# apt install strace python3
# cat > ex1.py
import json, sys
payload = [[x] * x for x in range(5, 10)] * 250
json.dump(payload, sys.stdout)
# strace python3 ex1.py >/dev/null
(uninteresting lines redacted)
write(1, "[[5, 5, 5, 5, 5], [6, 6, 6, 6, 6"..., 8194) = 8194
write(1, ", 6, 6], [7, 7, 7, 7, 7, 7, 7], "..., 8194) = 8194
write(1, "], [8, 8, 8, 8, 8, 8, 8, 8], [9,"..., 8194) = 8194
write(1, "[9, 9, 9, 9, 9, 9, 9, 9, 9], [5,"..., 4168) = 4168
# cat > ex2.py
import json, sys
payload = [[x] * x for x in range(5, 10)] * 250
pl2 = json.dumps(payload)
sys.stdout.write(pl2)
# strace python3 ex2.py
(uninteresting lines redacted)
write(1, "[[5, 5, 5, 5, 5], [6, 6, 6, 6, 6"..., 28750) = 28750