class ThreadUrl(threading.Thread):
def __init__(self, main_queue, source1_queue, source2_queue, source3_queue, source4_queue):
threading.Thread.__init__(self)
self.main_queue = main_queue
self.source1_queue = source1_queue
self.source2_queue = source2_queue
self.source3_queue = source3_queue
self.source4_queue = source4_queue
def run(self):
while True:
# 从任务队列里取出任务
sql_dict = self.main_queue.get()
sql = sql_dict.get('a')
if sql is not None:
self.source1_queue.put(sql)
self.main_queue.task_done()
else:
sql = sql_dict.get('b')
if sql is not None:
self.source2_queue.put(sql)
self.main_queue.task_done()
else:
sql = sql_dict.get('c')
if sql is not None:
self.source3_queue.put(sql)
self.main_queue.task_done()
else:
sql = sql_dict.get('d')
if sql is not None:
self.source4_queue.put(sql)
self.main_queue.task_done()
类似这种形式。
迷茫2017-04-18 09:51:41
The impact is not big, a clear structure is often more important than that little performance. If you want to optimize, you can use third-party tools (Pypy, etc.).
黄舟2017-04-18 09:51:41
Mainly because it’s ugly, such inelegant code.
else can be replaced by continue.
As for performance, are you sure you want to use threading.Thread?
阿神2017-04-18 09:51:41
It’s a bit verbose, write normal code in try, and handle sql exceptions in except
黄舟2017-04-18 09:51:41
Consider readability and maintainability first
Think about performance last