egg icon indicating copy to clipboard operation
egg copied to clipboard

多进程下process.pid每次打印都相同的问题

Open Zhu-jiayu opened this issue 6 years ago • 10 comments

What happens?

在单机多进程部署下, 通过this.app.uid="ABCD"; controller中, 打印process.pid 和 app.uid 为何每次都和上一次打印的一样? 不应该是按照进程随机的吗?

Zhu-jiayu avatar Sep 04 '20 04:09 Zhu-jiayu

用 curl 试,浏览器有 keep alive

atian25 avatar Sep 04 '20 05:09 atian25

试了curl, 结果还是一样的

Zhu-jiayu avatar Sep 04 '20 06:09 Zhu-jiayu

Hello @P3T3R-Z. Please provide a reproducible example following the instruction.

Issues labeled by Need Reproduce will be closed if no activities in 7 days.


@P3T3R-Z,请根据这个说明提供最小可复现代码。

如果在 7 天内没有进展会被自动关闭。

egg-bot avatar Sep 04 '20 07:09 egg-bot

schedule/test.js ctx.app赋值ABCD

module.exports = {
  schedule: {
    interval: "10s",
    type: "worker", 
    disable: false, 
    immediate: true,
  },
  async task(ctx) {
     ctx.app.uid='ABCD'
    console.log('from schedule====>', ctx.app.uid, process.pid)
  },
};

controller/test.js controller中打印app对象的uid

...
async index(){
   console.log('from controller--------->', this.ctx.app.uid, process.pid)
}

采用curl方式, 打印结果如下

from controller---------> undefined 18256
from schedule====> ABCD 6572
from controller---------> undefined 18256
from controller---------> undefined 18256
from controller---------> undefined 18256
from schedule====> ABCD 18256
from controller---------> ABCD 18256
from controller---------> ABCD 18256
from controller---------> ABCD 18256

Zhu-jiayu avatar Sep 04 '20 07:09 Zhu-jiayu

请按上面的提示,提交最小可复现仓库。

atian25 avatar Sep 04 '20 08:09 atian25

你是本地 dev 测试?

atian25 avatar Sep 04 '20 08:09 atian25

本地测试, 加了-c参数 egg-bin dev --port=8080 --sticky -c=4

Zhu-jiayu avatar Sep 04 '20 08:09 Zhu-jiayu

示例

Zhu-jiayu avatar Sep 04 '20 09:09 Zhu-jiayu

可能我描述的不太准确, 应该是每次打印的process.pid都是一样的

Zhu-jiayu avatar Sep 04 '20 09:09 Zhu-jiayu

你加了 --sticky 的原因

https://github.com/eggjs/egg-cluster/blob/63c3cd0dcbd5d3ca8d6b6c5bf08c85f4dd698d25/lib/master.js#L218

atian25 avatar Sep 06 '20 14:09 atian25