def search_from_database(self, number): thread_list = [] q1 = DB_Query() q2 = DB_Query() q3 = DB_Query() q4 = DB_Query() q5 = DB_Query() #余额查询 t1 = MyThread(q1.balance_query, args=(number, )) #通信消费查询 t2 = MyThread(q2.expenses_query, args=(number, )) #发展渠道 t3 = MyThread(q3.develop_type_query, args=(number, )) #充值查询 t4 = MyThread(q4.deposit_query, args=(number, )) #年龄查询 t5 = MyThread(q5.age_query, args=(number, )) thread_list = [t1, t2, t3, t4, t5] for t in thread_list: t.start() for t in thread_list: t.join() balance = t1.get_result() fee, sms_count, flow_data, call_duration = t2.get_result() develop_type = t3.get_result() last_deposit_fee, deposit_interval_day = t4.get_result() age = t5.get_result() return [ balance, fee, sms_count, flow_data, call_duration, develop_type, last_deposit_fee, age, deposit_interval_day ]
def get_baiduzd_faq(question): answers = [] # 返回值 question_list_html = None # 返回百度知道搜索结果的第一页 html 文本 while question_list_html is None: question_list_html = get_baiduzd_page(question) # 返回百度知道搜索结果的第一页 html 文本 if(question_list_html is None): print("爬取百度知道答案列表页面失败!") return None questions = get_sim_questions(question_list_html) # questions 为 问题句子-链接 字典 threadPool = [] # 线程队列 for question in questions: # 遍历每个问题链接,获取该问题的前 5 个答案 thread = MyThread(get_a_question_ans,args=(question['link'])) # 多线程加速 thread.start() threadPool.append(thread) for thread in threadPool: thread.join() for thread in threadPool: res = thread.get_result() if res is not None: answers += res return answers
def get_apps_info(self, url, classification, type): main_html = self.get_html(url) if main_html == None: print(url + "爬取失败!") return # 获取当前分类下的总页数 pageList_div = main_html.find("div", {"class": "pagelist", "id": "pagelist"}) pageList = pageList_div.find_all("a") #当前软件分类下的总页数 all_pages = int(pageList[len(pageList) - 2].get_text()) # 全局变量 global SHARE_Q threads = [] # 向队列中放入任务, 真正使用时, 应该设置为可持续的放入任务 for i in range(0, all_pages): current_url = url + "/" + str(i + 1) SHARE_Q.put(current_url) # 开启_WORKER_THREAD_NUM个线程 for i in range(_WORKER_THREAD_NUM): thread = MyThread(self.worker(classification, type)) # 线程开始处理任务 thread.start() threads.append(thread) for thread in threads: thread.join() # 等待所有任务完成 SHARE_Q.join()
def __init__(self): super().__init__() # 注意:里面的控件对象也成为窗口对象的属性了 self.setupUi(self) self.imgLine = np.zeros((h, w, 3), np.uint8) # 画布 self.imgBar = np.zeros((h, w, 3), np.uint8) self.figure = plt.figure() # 可选参数,facecolor为背景颜色 self.canvas = FigureCanvas(self.figure) self.__timer = QtCore.QTimer() # 定时器用于定时刷新 self.set_slot() self.__thread = UIFreshThread() # 开启线程(同时将这个线程类作为一个属性) MyThread(self.__thread, (), name='UIFreshThread', daemon=True).start() self.__timer.start(1000) # ms self.DeepList = [] self.NumList = []
def test_ip(self, ips): #检查代理ip可用性 print("检查代理ip可用性...共有" + str(len(ips)) + "个ip待检查") global SHARE_Q threads = [] # 向队列中放入任务, 真正使用时, 应该设置为可持续的放入任务 for task in range(0, len(ips)): SHARE_Q.put(ips[task]) # 开启_WORKER_THREAD_NUM个线程 for i in range(_WORKER_THREAD_NUM): thread = MyThread(self.worker) thread.start() # 线程开始处理任务 threads.append(thread) for thread in threads: thread.join() # 等待所有任务完成 SHARE_Q.join() print("可用代理ip:" + str(self.valid_ips))
def main(): init() threads = [None] * num_thread while True: print '' time.sleep(scan_interval) free_th_indexs = getFreeThreads(threads) print "free_th_indexs:", free_th_indexs if not free_th_indexs: continue wait_jobs = sql_opt.query() print "wait_jobs: ", wait_jobs if not wait_jobs: continue for i in range(len(free_th_indexs)): if i >= len(wait_jobs): break threads[free_th_indexs[i]] = MyThread(func, wait_jobs[i]) threads[free_th_indexs[i]].start()
def main(): set_hook() thread = MyThread() thread.start() time.sleep(5) thread.join()
# ch3/example1.py import sys sys.path.append( "'C:\\Users\\tnguy\\PycharmProjects\\MasteringConcurrency\\Chapter03\\my_thread.py" ) from my_thread import MyThread thread1 = MyThread('A', 0.5) thread2 = MyThread('B', 0.5) thread1.start() thread2.start() thread1.join() thread2.join() print('Finished.')
def on_tag_search(): tag_list = pic_manager.get_tags('Urban', 10) user_id = api_manager.get_user_id() thread = MyThread("urbanshot__", "kluza1", user_id, tag_list, 0)