脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 解决python线程卡死的问题

解决python线程卡死的问题

2021-05-29 00:25田野上的希望 Python

今天小编就为大家分享一篇解决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 . 服务进程数量不足导致的客户端进程卡死

服务端代码:

?
1
2
3
4
5
6
7
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()

客户端代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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个一直没有写入:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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线程卡死的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/u011734144/article/details/73287446

延伸 · 阅读

精彩推荐