Opened 9 years ago
Closed 9 years ago
#40 closed defect (fixed)
Bug on thread stop
Reported by: | zhangjr | Owned by: | yanll |
---|---|---|---|
Priority: | major | Milestone: | pre-alpha |
Component: | presenter | Version: | 0.1 |
Keywords: | Cc: | ||
Blocked By: | Blocking: | ||
Parent ID: | Parent Tickets: | ||
Estimated Number of Hours: | 0.0 | Add Hours to Ticket: | 0 |
Billable?: | yes | Total Hours: | 0 |
Description
Example:
import threading
class StoppableThread?(threading.Thread):
"""Thread class with a stop() method. The thread itself has to check
regularly for the stopped() condition."""
def init(self):
super(StoppableThread?, self).init()
self._stop = threading.Event()
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
Subtickets
Change History (6)
comment:1 Changed 9 years ago by zhangjr
comment:2 Changed 9 years ago by zhangjr
Productor-cosumer model is applied.
Productor has in- and out- queue.
Cosumer has in-queue.
Queue can support FIFO, LIFO, and Priority modes.
comment:3 Changed 9 years ago by zhangjr
Changes in Thread.run()
===
try:
self.process
except:
break
===
comment:4 Changed 9 years ago by zhangjr
Another possible solution. weakref+with
===
import weakref
class FooType?(object):
def init(self, id, parent):
self.id = id
self.parent = weakref.ref(parent)
#self.parent =parent
print 'child', self.id, 'new'
def del(self):
print 'child', self.id, 'del'
def exit(self, type, value, traceback):
print 'child', self.id, 'exit'
def enter(self):
print 'child', self.id, 'enter'
class BarType?(object):
def init(self, id):
self.id = id
print 'main', self.id, 'new'
with FooType?(id, self) as self.foo:
pass
def del(self):
print 'main', self.id, 'del'
b = BarType?(12)
===
comment:5 Changed 9 years ago by zhangjr
The final solution:
try:
except:
finally:
comment:6 Changed 9 years ago by zhangjr
- Resolution set to fixed
- Status changed from new to closed
Could be solution.
===
import Queue # or queue in Python 3
import threading
class PrintThread?(threading.Thread):
class ProcessThread?(threading.Thread):
pathqueue = Queue.Queue()
resultqueue = Queue.Queue()
paths = getThisFromSomeWhere()
output = codecs.open('file', 'a')
# spawn threads to process
for i in range(0, 5):
# spawn threads to print
t = PrintThread?(resultqueue)
t.setDaemon(True)
t.start()
# add paths to queue
for path in paths:
# wait for queue to get empty
pathqueue.join()
resultqueue.join()
===