Python中文
首页
教程
问答
标签
搜索
登录
注册
多处理队列.get()挂起
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我正在尝试实现基本的多处理,我遇到了一个问题。python脚本附在下面。</p> <pre><code>import time, sys, random, threading from multiprocessing import Process from Queue import Queue from FrequencyAnalysis import FrequencyStore, AnalyzeFrequency <a href="https://www.cnpython.com/list/append" class="inner-link">append</a>_queue = Queue(10) database = FrequencyStore() def add_to_append_queue(_list): append_queue.put(_list) def process_append_queue(): while True: item = append_queue.get() database.append(item) print("Appended to database in %.4f seconds" % database.append_time) append_queue.task_done() return def main(): database.load_db() print("Database loaded in %.4f seconds" % database.load_time) append_queue_process = Process(target=process_append_queue) append_queue_process.daemon = True append_queue_process.start() #t = threading.Thread(target=process_append_queue) #t.daemon = True #t.start() while True: path = raw_input("file: ") if path == "exit": break a = AnalyzeFrequency(path) a.analyze() print("Analyzed file in %.4f seconds" % a._time) add_to_append_queue(a.get_results()) append_queue.join() #append_queue_process.join() database.save_db() print("Database saved in %.4f seconds" % database.save_time) sys.exit(0) if __name__=="__main__": main() </code></pre> <p>AnalyzeFrequency分析文件中单词的频率,<code>get_results()</code>返回所述单词和频率的排序列表。这个单子很大,大概有一万件。</p> <p>然后将此列表传递给<code>add_to_append_queue</code>方法,该方法将其添加到队列中。process_append_队列逐个获取项目并将频率添加到“数据库”中。这个操作比<code>main()</code>中的实际分析要长一点,因此我尝试对这个方法使用单独的进程。当我尝试对线程模块执行此操作时,一切都工作得很好,没有错误。当我尝试使用Process时,脚本挂在<code>item = append_queue.get()</code>。</p> <p>有人能解释一下这里发生了什么事吗,也许能帮我找个解决办法?</p> <p>感谢所有答案!</p> <p><strong>更新</strong></p> <p><strong>泡菜错误是我的错,只是打字错误。现在我在多处理中使用Queue类,但是append_Queue.get()方法仍然挂起。 新代码</strong></p> <pre><code>import time, sys, random from multiprocessing import Process, Queue from FrequencyAnalysis import FrequencyStore, AnalyzeFrequency append_queue = Queue() database = FrequencyStore() def add_to_append_queue(_list): append_queue.put(_list) def process_append_queue(): while True: database.append(append_queue.get()) print("Appended to database in %.4f seconds" % database.append_time) return def main(): database.load_db() print("Database loaded in %.4f seconds" % database.load_time) append_queue_process = Process(target=process_append_queue) append_queue_process.daemon = True append_queue_process.start() #t = threading.Thread(target=process_append_queue) #t.daemon = True #t.start() while True: path = raw_input("file: ") if path == "exit": break a = AnalyzeFrequency(path) a.analyze() print("Analyzed file in %.4f seconds" % a._time) add_to_append_queue(a.get_results()) #append_queue.join() #append_queue_process.join() print str(append_queue.qsize()) database.save_db() print("Database saved in %.4f seconds" % database.save_time) sys.exit(0) if __name__=="__main__": main() </code></pre> <p><strong>更新2</strong></p> <p>这是数据库代码:</p> <pre><code>class FrequencyStore: def __init__(self): self.sorter = Sorter() self.db = {} self.load_time = -1 self.save_time = -1 self.append_time = -1 self.sort_time = -1 def load_db(self): start_time = time.time() try: file = open("results.txt", 'r') except: raise IOError self.db = {} for line in file: word, count = line.strip("\n").split("=") self.db[word] = int(count) file.close() self.load_time = time.time() - start_time def save_db(self): start_time = time.time() _db = [] for key in self.db: _db.append([key, self.db[key]]) _db = self.sort(_db) try: file = open("results.txt", 'w') except: raise IOError file.truncate(0) for x in _db: file.write(x[0] + "=" + str(x[1]) + "\n") file.close() self.save_time = time.time() - start_time def create_sorted_db(self): _temp_db = [] for key in self.db: _temp_db.append([key, self.db[key]]) _temp_db = self.sort(_temp_db) _temp_db.reverse() return _temp_db def get_db(self): return self.db def sort(self, _list): start_time = time.time() _list = self.sorter.mergesort(_list) _list.reverse() self.sort_time = time.time() - start_time return _list def append(self, _list): start_time = time.time() for x in _list: if x[0] not in self.db: self.db[x[0]] = x[1] else: self.db[x[0]] += x[1] self.append_time = time.time() - start_time </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p><code>queue.Queue</code>是线程安全的,但不能跨进程工作。不过,这很容易解决。而不是:</p> <pre><code>from multiprocessing import Process from Queue import Queue </code></pre> <p>你想要:</p> <pre><code>from multiprocessing import Process, Queue </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
Python中两个字典的交集
1 回答
python中两个字符串上的异或操作数?
6 回答
Python中两个字符串中的类似句子
3 回答
Python中两个字符串之间的Hamming距离
5 回答
python中两个字符串之间的匹配模式
10 回答
python中两个字符串之间的按位或
8 回答
python中两个字符串之间的数据(字节)切片
6 回答
python中两个字符串之间的模式
3 回答
python中两个字符串作为子字符串的区别
10 回答
Python中两个字符串元组的比较
6 回答
Python中两个字符串列表中的公共字符串
6 回答
python中两个字符串的Anagram测试
4 回答
Python中两个字符串的正则匹配
5 回答
python中两个字符串的笛卡尔乘积
2 回答
Python中两个字符串相似性的比较
6 回答
python中两个字符串语义相似度的求法
5 回答
Python中两个字符置换成固定长度的字符串,每个字符的数目相等
6 回答
Python中两个对数方程之间的插值和平滑数据
10 回答
Python中两个对象之间的And/Or运算符
5 回答
python中两个嵌套字典中相似键的和值
3 回答