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

Could be solution.

===
import Queue # or queue in Python 3
import threading

class PrintThread?(threading.Thread):

def init(self, queue):

threading.Thread.init(self)
self.queue = queue

def printfiles(self, p):

for path, dirs, files in os.walk(p):

for f in files:

print(f, file=output)

def run(self):

while True:

result = self.queue.get()
self.printfiles(result)
self.queue.task_done()

class ProcessThread?(threading.Thread):

def init(self, in_queue, out_queue):

threading.Thread.init(self)
self.in_queue = in_queue
self.out_queue = out_queue

def run(self):

while True:

path = self.in_queue.get()
result = self.process(path)
self.out_queue.put(result)
self.in_queue.task_done()

def process(self, path):

# Do the processing job here

pathqueue = Queue.Queue()
resultqueue = Queue.Queue()
paths = getThisFromSomeWhere()

output = codecs.open('file', 'a')

# spawn threads to process
for i in range(0, 5):

t = ProcessThread?(pathqueue, resultqueue)
t.setDaemon(True)
t.start()

# spawn threads to print
t = PrintThread?(resultqueue)
t.setDaemon(True)
t.start()

# add paths to queue
for path in paths:

pathqueue.put(path)

# wait for queue to get empty
pathqueue.join()
resultqueue.join()
===

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

===

Last edited 9 years ago by zhangjr (previous) (diff)

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)
===

Last edited 9 years ago by zhangjr (previous) (diff)

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
Note: See TracTickets for help on using tickets.