怎么在openresty实现并发操作?

76 views
Skip to first unread message

Lucas Hwang

unread,
Jul 24, 2022, 11:35:51 PM7/24/22
to openresty
DD03C797-39D2-4060-969A-F65F0A263E61.png
WechatIMG1817.png
如上图所示,我想用spawn同时执行两个函数,为什么这边却有阻塞的效果呢,每个函数执行1s,如果是真正的并发应该是1s吧,为什么这里是2s呢。如果要实现真正的并发操作,要怎么做的呢?

Jiteng Wong

unread,
Jul 25, 2022, 1:33:06 AM7/25/22
to openresty
试试这样做的总时间是多少?
starttime = os.clock()
ngx.thread.spawn(test, 1)
ngx.thread.spawn(test, 1)
endtime =  os.clock()

ngx.say(string.format("cost time: %.4f", endtime - starttime))

Lucas Hwang

unread,
Jul 25, 2022, 1:51:48 AM7/25/22
to openresty
cost time  : 2.0004
curl http://localhost/api/parallel_judge_test.json  0.01s user 0.01s system 0% cpu 2.069 total
时间不变

Jiteng Wong 在 2022年7月25日 星期一下午1:33:06 [UTC+8] 的信中寫道:

Junlong li

unread,
Jul 25, 2022, 8:20:06 PM7/25/22
to open...@googlegroups.com
因为实际上只有一个物理线程在执行。如果想要两个线程需要使用lua-resty-shell或者run_worker_thread的功能。

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
---
您收到此邮件是因为您订阅了Google网上论坛上的“openresty”群组。
要退订此群组并停止接收此群组的电子邮件,请发送电子邮件到openresty+...@googlegroups.com
要在网络上查看此讨论,请访问https://groups.google.com/d/msgid/openresty/6e1096ed-0f38-484a-bb73-e3950adb7ef8n%40googlegroups.com


--
-
Reply all
Reply to author
Forward
0 new messages