我正在写一个简单的应用程序,它从文件中读取(大约一百万行)行,将这些行复制到列表中,如果下一行与前一行不同,它将运行一个线程,以对该列表执行一些工作。线程作业基于tcp套接字,通过telnet lib发送和接收命令。
有时我的应用程序挂起,什么也不做。
所以如果你能追踪到我那就太好了。
对于这个文件,我创建了40个线程。伪代码看起来:
lock = threading.Lock()
no_of_jobs = 0
class DoJob(threading.Thread):
def start(self, cond, work):
self.work = work
threading.Thread.start(self)
def run(self)
global lock
global no_of_jobs
lock.acquire()
no_of_jobs += 1
lock.release()
# do some job, if error or if finished, decrement no_of_jobs under lock
(...)
main:
#starting conditions:
with open(sys.argv[1]) as targetsfile:
head = [targetsfile.next() for x in xrange(1)]
s = head[0]
prev_cond = s[0]
work = []
for line in open(sys.argv[1], "r"):
cond = line([0])
if prev_cond != cond:
while(no_of_jobs>= MAX_THREADS):
time.sleep(1)
DoJob(cond, work)
prev_cond = cond
work = None
work = []
work.append(line)
#last job:
DoJob(cond, work)
while threading.activeCount() > 1:
time.sleep(1)
向J致以最好的敬意
发布于 2012-12-07 13:01:29
我曾经成功地使用过如下代码(来自我编写的python 3程序):
import threading
def die():
print('ran for too long. quitting.')
for thread in threading.enumerate():
if thread.isAlive():
try:
thread._stop()
except:
pass
sys.exit(1)
if __name__ == '__main__':
#bunch of app-specific code...
# setup max runtime
die = threading.Timer(2.0, die) #quit after 2 seconds
die.daemon = True
die.start()
#after work is done
die.cancel()
https://stackoverflow.com/questions/13763446
复制相似问题