How to run python thread on only one Paste server?

209 views
Skip to first unread message

Radosław Trojanowski

unread,
Aug 29, 2013, 5:55:18 AM8/29/13
to pylons-...@googlegroups.com
Hello guys,

I have got little problem during developing notification functionality in Pylons-based application.
We use multi-server configuration with 4 Paste servers (with 10-threads threadpool for each server).
I decided to implement is as background thread which through scheduler  runs check for notifications every minute.

in Python script I imported:

import thread, threading
 
 and in main make_app function added following:

thread.start_new_thread(timingforalerts, (app_globals, config, ))             #creates separate thread for searching pending alerts

timingforalerts() is my function which queries database for pending alerts and if any are found sends notifications through e-mail/jabber.

PROBLEM is that this thread is started on EACH server, so that means there are FOUR threads created for searching alerts. It affects that when notification is found user receives four email or four Jabber messages.

Please help me figure out how to start thread only on one server (for example only on server:main).

Configuration in development.ini looks following:

[server:main]
use = egg:Paste#http
host = 127.0.0.1
port = 5000
use_threadpool = True
threadpool_workers = 10

[server:main2]
use = egg:Paste#http
host = 127.0.0.1
port = 5001
use_threadpool = True
threadpool_workers = 10

[server:main3]
use = egg:Paste#http
host = 127.0.0.1
port = 5002
use_threadpool = True
threadpool_workers = 10

[server:main4]
use = egg:Paste#http
host = 127.0.0.1
port = 5003
use_threadpool = True
threadpool_workers = 10 

Michael Merickel

unread,
Aug 29, 2013, 10:57:03 AM8/29/13
to Pylons
Your worker should be a separate process, run as a script. When run individually you have very simple control over how many wsgi servers and how many workers you are running. They really have no business being coupled.

Of course you could hack it with some CLI flag passed into your INI file.

[app:main]
...
spawn_worker = %(spawn_worker)s

paster serve my.ini spawn_worker=true
paster serve my.ini spawn_worker=false

But really, just make it a separate process.

- Michael


--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To post to this group, send email to pylons-...@googlegroups.com.
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/groups/opt_out.

Radosław Trojanowski

unread,
Aug 29, 2013, 11:13:01 AM8/29/13
to pylons-...@googlegroups.com
Hi, as 'new process' do you mean I should rewrite notification part to separate .py file, add in development.ini new application (for example [app:notifications] and run it separately from main app?
Thanks for reply.

Regards,
Radek

Pozdrawiam,
Radosław Trojanowski


2013/8/29 Michael Merickel <mmer...@gmail.com>

--
You received this message because you are subscribed to a topic in the Google Groups "pylons-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pylons-discuss/-7ZIlMAjKhk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pylons-discus...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages