简体   繁体   中英

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.

I've got everything working but the last piece remains when they have to signup via oauth to get that bot actually started.

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:

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

That works well running via a Python daemon .

I also serve an oauth url in order to sign up the team as discussed above.

api.add_resource(OAuth, '/oauth')

The problem is that this would be running under a uwsgi server, under a different process. How can it possibly instantiate a new bot for the team that just signed up in the same process as every other 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.

I'm not sure if this problem can be solved directly with gevent library. I needed somehow a pub sub pattern between the uwsgi process and the daemon.

Hence I tried to solve this by using pgpubsub https://bitbucket.org/btubbs/pgpubsub . However it works only within the same thread, and gevent breaks it.

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. There's an example in my ToDo app at https://bitbucket.org/btubbs/todopy-pg , which uses both Gevent and pgpubsub.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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