guys!
My application is a bot. It simply receives a message, process it and returns result.
But there are a lot of messages and I'm creating separate thread for processing each, but it makes an application slower (not a bit).
So, Is it any way to reduce CPU usage by replacing threads with something else?
-
@david-heffernan Looks like. Threads should sync their data and there are cpu costs. Creating and destroying threads costs CPU too.Vladimir Protasov– Vladimir Protasov2011-02-20 23:52:31 +00:00Commented Feb 20, 2011 at 23:52
4 Answers
You probably want processes rather than threads. Spawn processes at startup, and use Pipes to talk to them.
1 Comment
Threads and processes have the same speed. Your problem is not which one you use, but how many you use.
The answer is to only have a fixed couple of threads or processes. Say 10. You then create a Queue (use the Queue module) to store all messages from your robot. The 10 threads will constantly be working, and everytime they finish, they wait for a new message in the Queue.
This saves you from the overhead of creating and destroying threads. See http://docs.python.org/library/queue.html for more info.
def worker():
while True:
item = q.get()
do_work(item)
q.task_done()
q = Queue()
for i in range(num_worker_threads):
t = Thread(target=worker)
t.daemon = True
t.start()
for item in source():
q.put(item)
q.join() # block until all tasks are done
2 Comments
You might not even need threads. If your server can handle each request quickly, you can just make it all single-threaded using something like Twisted.