解决python线程卡死的问题

yipeiwu_com6年前Python基础

1. top命令和日志方式判定卡死的位置

python代码忽然卡死,日志不输出,通过如下方式可以确定线程确实已经死掉了:

# top 命令

top命令可以看到机器上所有线程的执行情况,%CPU和%MEM可以看出线程消耗的资源情况

由于机器上线程数量太多,可能要查看的线程的信息在top命令当前屏幕上显示不出来可以通过如下方式查看

在top命令下输入:u

接下来会提示输入用户名,就可以查看该用户所执行的所有线程

Which user (blank for all): denglinjie

这样就可以看到degnlinjie用户的所有线程

可以看到那几个卡死线程的%CPU和%MEM都为0,说明线程根本没有消耗资源,那么可以看出线程已经卡死了

接下来通过打日志的方式来确定线程究竟是卡死在哪里了,线程卡死的地方大多数都是在io或者http请求那,所以以后遇到线程卡死的情况,就通过打日志的方式来确定卡死的位置,最终定位到问题确实是一个http服务挂掉了,而且此时requests.get()我虽然设置了超时,但是竟然无效

2 . 服务进程数量不足导致的客户端进程卡死

服务端代码:

handler = SimilarityService()
transport = TSocket.TServerSocket('10.134.113.75', 1234)
factory = TBinaryProtocol.TBinaryProtocolFactory()
processor = Processor(handler)
server = TProcessPoolServer.TProcessPoolServer(processor, transport)
server.setNumWorkers(10)
server.serve()

客户端代码

docQue = queues.Queue(maxsize=1000)
pCount = 15
 
 
class ParseSaveEsProcess(multiprocessing.Process):
 
  def __init__(self, threadId):
    self.threadId = threadId
    multiprocessing.Process.__init__(self)
 
  def run(self):
    global docQue
    f = open('recall_match_file_all_simi.lst.%s' % self.threadId, 'w')
    try:
      transport = TSocket.TSocket('10.134.113.75', 1234)
      transport = TTransport.TBufferedTransport(transport)
      protocol = TBinaryProtocol.TBinaryProtocol(transport)
      client = Client(protocol)
      transport.open()
 
      while True:
        line = docQue.get(block=True)
        if not line:
          print 'thread%d run over' % self.threadId  
          break
 
        p = line.split('\t')
        if len(p) >= 6 and p[5] == 'simi_high':
          simi_str = client.calculate_similarity_by_itemurl(p[0])
          f.write(line + '\t' + simi_str + '\n')
        else:
          f.write(line + '\n')  
      transport.close()
    except Thrift.TException as e:
      print str(e)
      pass  
 
class PutUrlProcess(multiprocessing.Process):
 
  def __init__(self):
    multiprocessing.Process.__init__(self)
 
  def run(self):
    global docQue
    for line in open('recall_match_file.lst', 'r'):
      baikeid = line.strip()
      docQue.put(baikeid, block=True)
 
    for i in range(pCount):
      docQue.put(None, block=True)
 
 
if __name__ == '__main__':
  putProcess = PutUrlProcess()
  putProcess.start()
 
  for i in range(pCount):
    parseProcess = ParseSaveEsProcess(i)
    parseProcess.start()

可以看到,进程ParseSaveEsProcess进程总共开启了15个,每个进程会打开一个thrift连接,打开后一直发送请求,并将处理的结果写文件,全部执行完成后才关闭thrift连接。

可是,发现从启动到执行了很长时间后,只有10个文件里面有内容写入,其中5个一直没有写入:

111965 recall_match_file_all_simi.lst.0
  111878 recall_match_file_all_simi.lst.1
    0 recall_match_file_all_simi.lst.10
    0 recall_match_file_all_simi.lst.11
    0 recall_match_file_all_simi.lst.12
    0 recall_match_file_all_simi.lst.13
    0 recall_match_file_all_simi.lst.14
  113429 recall_match_file_all_simi.lst.2
  110720 recall_match_file_all_simi.lst.3
  111993 recall_match_file_all_simi.lst.4
  113691 recall_match_file_all_simi.lst.5
  113360 recall_match_file_all_simi.lst.6
  113953 recall_match_file_all_simi.lst.7
  112007 recall_match_file_all_simi.lst.8
  113818 recall_match_file_all_simi.lst.9

原因是因为thrift服务端只启动了10个服务进程,所以只能同时处理10个请求,而我客户端打开的thrift连接一直没有关闭,所以10个服务进程被10个客户端连接霸占了,另外5个进程获取不到连接,自然就一直卡住了。

以上这篇解决python线程卡死的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python判断字符串是否是json格式方法分享

在实际工作中,有时候需要对判断字符串是否为合法的json格式 解决方法使用json.loads,这样更加符合‘Pythonic'写法 代码示例: Python import json...

Python编程深度学习计算库之numpy

Python编程深度学习计算库之numpy

NumPy是python下的计算库,被非常广泛地应用,尤其是近来的深度学习的推广。在这篇文章中,将会介绍使用numpy进行一些最为基础的计算。 NumPy vs SciPy NumPy和...

python gensim使用word2vec词向量处理中文语料的方法

python gensim使用word2vec词向量处理中文语料的方法

word2vec介绍 word2vec官网:https://code.google.com/p/word2vec/ word2vec是google的一个开源工具,能够根据输入的词...

python 随机森林算法及其优化详解

前言 优化随机森林算法,正确率提高1%~5%(已经有90%+的正确率,再调高会导致过拟合) 论文当然是参考的,毕竟出现早的算法都被人研究烂了,什么优化基本都做过。而人类最高明之处就是懂...

Python中SOAP项目的介绍及其在web开发中的应用

SOAP.py 客户机和服务器 SOAP.py 包含的是一些基本的东西。没有 Web 服务描述语言(Web Services Description Language,WSDL)或者任何...