简体   繁体   English

如何从一个过程向另一个过程发出信号?

[英]How to signal from one process to another?

I'm writing a Slack bot, where teams can sign up to add that bot to serve their team. 我正在编写一个Slack机器人,团队可以在其中注册以添加该机器人来为其团队服务。

I've got everything working but the last piece remains when they have to signup via oauth to get that bot actually started. 我已经完成了所有工作,但是最后一件事情仍然存在,当他们必须通过oauth进行注册才能真正启动该机器人时。

I initially instantiate a bot for each team this way: 我最初以这种方式为每个团队实例化一个机器人:

teams = self.session.query(Team).all()
for team in teams:
    bot = RtmBot(team.bot_access_token, team.bot_user_id)
    self.bots.append(bot)

Then I run the bots within nonblocking gevent: 然后,我在非阻塞gevent中运行这些机器人:

for bot in self.bots:
    events.append(gevent.spawn(bot.start))
gevent.joinall(events)

That works well running via a Python daemon . 通过Python daemon可以很好地运行。

I also serve an oauth url in order to sign up the team as discussed above. 我还提供了一个oauth网址,以便如上所述注册该团队。

api.add_resource(OAuth, '/oauth')

The problem is that this would be running under a uwsgi server, under a different process. 问题是这将在uwsgi服务器下以不同的进程运行。 How can it possibly instantiate a new bot for the team that just signed up in the same process as every other bot? 对于刚刚以与其他所有bot相同的过程进行了注册的团队,该如何为该实例化一个新的bot?

I possibly need to do something like this: 我可能需要做这样的事情:

Instantiate a new bot for the new team: 为新团队实例化新的机器人:

bot = RtmBot(team.bot_access_token, team.bot_user_id)

and then spawn that: 然后生成:

gevent.spawn(bot.start)

But if I did that within the context of uwsgi, that bot won't be running within the context of daemon. 但是,如果我在uwsgi的上下文中执行此操作,则该bot将不会在守护程序的上下文中运行。

I'm not sure if this problem can be solved directly with gevent library. 我不确定是否可以使用gevent库直接解决此问题。 I needed somehow a pub sub pattern between the uwsgi process and the daemon. 我需要在uwsgi进程和守护程序之间建立pub子模式。

Hence I tried to solve this by using pgpubsub https://bitbucket.org/btubbs/pgpubsub . 因此,我尝试使用pgpubsub https://bitbucket.org/btubbs/pgpubsub解决此问题。 However it works only within the same thread, and gevent breaks it. 但是,它只能在同一线程中工作,而gevent会破坏它。

Any ideas please? 有什么想法吗?

You can use Gevent with pgpubsub if you install the 'psycogreen' package and call psycogreen.gevent.patch_psycopg() to make the psycopg2 driver play nicely with Gevent. 如果安装了'psycogreen'软件包并调用psycogreen.gevent.patch_psycopg(),可以使Gevent与pgpubsub一起使用,以使psycopg2驱动程序在Gevent中正常播放。 There's an example in my ToDo app at https://bitbucket.org/btubbs/todopy-pg , which uses both Gevent and pgpubsub. 我的ToDo应用程序中有一个示例,位于https://bitbucket.org/btubbs/todopy-pg ,它同时使用Gevent和pgpubsub。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM