Recent Posts
XML | 10 sec ago
None | 18 sec ago
None | 24 sec ago
Bash | 25 sec ago
JavaScript | 25 sec ago
None | 51 sec ago
XML | 51 sec ago
Bash | 1 min ago
None | 1 min ago
None | 1 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By corey g on the 5th of Feb 2009 09:04:12 PM Download | Raw | Embed | Report
  1. import threading
  2. import Queue
  3. import time
  4.  
  5.  
  6. class ResultWriter(threading.Thread):
  7.     def __init__(self, queue):
  8.         threading.Thread.__init__(self)
  9.         self.q = queue    
  10.  
  11.     def run(self):
  12.         f = open('foo.txt', 'a')
  13.         while True:
  14.             try:
  15.                 item = self.q.get(False)
  16.                 f.write(item)
  17.                 f.flush()
  18.             except Queue.Empty:
  19.                 # re-check queue for messages every x sec
  20.                 time.sleep(.01)
  21.            
  22.                
  23. class Foo(threading.Thread):
  24.     def __init__(self):
  25.         threading.Thread.__init__(self)
  26.    
  27.     def run(self):
  28.         q = Queue.Queue(0)
  29.  
  30.         t = ResultWriter(q)
  31.         t.setDaemon(True)
  32.         t.start()
  33.  
  34.         while True:
  35.             print 'foo'
  36.             q.put('foo')
  37.  
  38.  
  39. f = Foo()
  40. f.setDaemon(True)
  41. f.start()
  42. while True:
  43.     time.sleep(.1)
Submit a correction or amendment below. Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: