import sys, re, os, random, math, imp, threading, gc, datetime, time
import jsonrpclib
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCRequestHandler
import SocketServer
import logging
class ThreadedJSONRPCServer(SocketServer.ThreadingMixIn, SimpleJSONRPCServer):
pass
class myRequestHandler(SimpleJSONRPCRequestHandler):
rpc_paths=('/test')
clientIp=None
def do_OPTIONS(self):
self.send_response(200)
self.end_headers()
def do_GET(self):
self.send_response(200)
self.end_headers()
def do_POST(self):
# global clientIp
# clientIp, clientPort=self.client_address
SimpleJSONRPCRequestHandler.do_POST(self)
def end_headers(self):
self.send_header('Access-Control-Allow-Headers', 'Origin, Authorization, X-Requested-With, Content-Type, Accept')
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Max-Age', '0')
self.send_header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS')
SimpleJSONRPCRequestHandler.end_headers(self)
class mySharedMethods:
def test(self):
# this method do nothing!
return time.sleep(2) # without sleep i'm need waiting more time before problem happened
if __name__=='__main__':
print 'Running Api...'
logging.getLogger('jsonrpclib').setLevel(logging.DEBUG)
server=ThreadedJSONRPCServer(("0.0.0.0", 8099), requestHandler=myRequestHandler,logRequests=False)
server.register_instance(mySharedMethods())
server.serve_forever()
on this server i'm give high load (near 5-15 connections per second).
Because i'm use ThreadedJSONRPCServer(), for every incoming connection creating new thread. In htop with tree view i'm see them very well. Threads added and removed, all work well. Every thread eat ~0.1% of cpu.
BUT..
after 10-60 minutes one(sometimes two or three) threads not removed. I'm name them 'Ghosts'.
Each Ghost eat near 50% cpu. And it never stop this and it never removed.
After first Ghost happened, process start eating memory.
After 2-3 hours process takes 6-7 gigabytes of RAM.
Peak is 22 gigabytes.
I'm think i'm do something wrong but i can't understand what...
p.s. If i'm use SimpleJSONRPCServer() instead ThreadedJSONRPCServer() all work so sloooow. And so many timeouted connections...