tcalmant / jsonrpclib

A Python (2 & 3) JSON-RPC over HTTP that mirrors the syntax of xmlrpclib (aka jsonrpclib-pelix)
https://jsonrpclib-pelix.readthedocs.io/
Apache License 2.0
53 stars 24 forks source link

Pooled don't start via multiprocessing.Process #19

Closed byaka closed 9 years ago

byaka commented 9 years ago

It's not a big problem, but strange..

from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer
from jsonrpclib.SimpleJSONRPCServer import PooledJSONRPCServer
from jsonrpclib.threadpool import ThreadPool
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCRequestHandler
import jsonrpclib
import jsonrpclib.utils
try: import xmlrpc.server as xmlrpcserver
except ImportError: import SimpleXMLRPCServer as xmlrpcserver

from multiprocessing import Process

class myRequestHandler(SimpleJSONRPCRequestHandler):
   rpc_paths=('/adWolf-redirectApi')

class mySharedMethods:
   def ping(self): return 'pong'

if __name__=='__main__':
   print 'Running api..'
   # server=SimpleJSONRPCServer(("0.0.0.0", 8014), requestHandler=myRequestHandler, logRequests=False)
   request_pool=ThreadPool(max_threads=50, min_threads=10)
   request_pool.start()
   server=PooledJSONRPCServer(("0.0.0.0", 8014), thread_pool=request_pool, requestHandler=myRequestHandler, logRequests=False)
   server.register_instance(mySharedMethods())
   # server.serve_forever()
   Process(target=server.serve_forever).start()

This code work without errors, but i can't connect to server. But if i change PooledJSONRPCServer to SimpleJSONRPCServer, all work good.

tcalmant commented 9 years ago

The problem is simple: the thread pool is in the parent process, not the child one. When calling Process() (or fork() for that matter), you only fork the calling thread, not the others.

You could try either to create the server in the child process, or to write a method starting the pool then calling server_forever and to call Process on it.

byaka commented 9 years ago

Thx