Explorar el Código

fix the blocking queue

David hace 10 años
padre
commit
d3b48d5dc0
Se han modificado 1 ficheros con 14 adiciones y 8 borrados
  1. 14 8
      parser.py

+ 14 - 8
parser.py

@@ -35,12 +35,10 @@ def parseSource(s):
             t.start()
             threads.append(t)
     
-        if (q.qsize() > 0):
-            print("Waiting for %d queue elements (0 if num_workers > new_articles)" % q.qsize())
-            q.join() #block until all tasks are done
-            print("Finished waiting for the queue")
+        print("Waiting for %d queue elements" % q.qsize())
+        q.join() #block until all tasks are done
+        print("Finished waiting for the queue")
 
-        #stop workers
         for i in range(num_worker_threads):
             q.put(None)
         print("Waiting for %d threads" % len(threads))
@@ -49,14 +47,22 @@ def parseSource(s):
     print("[Source] %s => Finished parsing" %s["name"])
 
 def news_worker(source_id,kw):
+    tname=threading.current_thread().name
     while True:
-        item = q.get()
+        try:
+            item = q.get(timeout=5)
+        except:
+            print("[%s] Empty queue" % tname)
+            break
         if item is None:
+            print("[%s] Empty queue (none)" % tname)
+            q.task_done()
             break
-        print("[Thread %s] %s" % (threading.current_thread().name,item))
+
+        print("[Thread %s] %s" % (tname,item))
         try:
             n=News(item,source_id,kw)
-            print("[Thread %s] Finished" % threading.current_thread().name)
+            print("[Thread %s] Finished" % tname)
             d.insert_article(n.get())
         except:
             print("whut")