7 Replies - 4537 Views - Last Post: 17 July 2010 - 08:35 AM Rate Topic: -----

#1 chemicalfan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 88
  • Joined: 16-October 09

SocketServer module + multiprocessing?

Posted 15 July 2010 - 03:55 AM

I know about the ForkingMixIn and ThreadingMixIn for the SocketServer module, but is there a multiprocessing mix-in for it? I can't see it documented in the standard library, so I guess I'm going to have to look elsewhere? Basically, I want to use multiprocessing, so that "true" multithreading (i.e. getting round the GIL) is possible on Windows systems.

I'm trying to build a server app, and I wanted each concurrent client connection to spawn a new process, so on a multithreaded server, the connections receive the best possible performance.

Is This A Good Question/Topic? 0
  • +

Replies To: SocketServer module + multiprocessing?

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: SocketServer module + multiprocessing?

Posted 15 July 2010 - 05:19 AM

While this is on the fringe of my knowledge of Python's inner workings, I have the following thoughts:

Wouldn't using the ForkingMixIn leverage the power of multiple cores/processors? If I am thinking about this properly, each sub-process would act on its own virtual machine, each having its own GIL. Reading through the docs for ForkingMixIn and multiprocessing, the two seem quite similar.

If the ForkingMixIn does not work out for you (does the ForkingMixIn work on Windows?), you will probably have to roll your own server code with asyncore and multiprocessing.

This post has been edited by Motoma: 15 July 2010 - 05:21 AM

Was This Post Helpful? 0
  • +
  • -

#3 chemicalfan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 88
  • Joined: 16-October 09

Re: SocketServer module + multiprocessing?

Posted 15 July 2010 - 02:09 PM

Forking isn't supported under Windows - it's Unix exclusive :(

Is there a problem with connecting a Windows client to a Unix/Linux server using sockets? If not, I'll probably go with forking, and enforce the server app run on a Linux machine. I'm not keen on "going it alone" and coding support for multiprocessing myself - I've actually never coded a multithreaded app as it is. It's all learning, it's all fun :)
Was This Post Helpful? 0
  • +
  • -

#4 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: SocketServer module + multiprocessing?

Posted 15 July 2010 - 02:19 PM

View Postchemicalfan, on 15 July 2010 - 03:09 PM, said:

Forking isn't supported under Windows - it's Unix exclusive :(

Is there a problem with connecting a Windows client to a Unix/Linux server using sockets? If not, I'll probably go with forking, and enforce the server app run on a Linux machine. I'm not keen on "going it alone" and coding support for multiprocessing myself - I've actually never coded a multithreaded app as it is. It's all learning, it's all fun :)


No, there's no problem running Windows clients against a Linux server. You could always code your server in a way that checks for ForkingMixIn and uses it if it can, but reverts to ThreadingMixIn if it cannot. This would allow you to have a fully cross-platform system; you could then include a disclaimer in the documentation stating that the performance is better when running a Linux server (if it indeed is!). This will give your users both choice and flexibility.
Was This Post Helpful? 1
  • +
  • -

#5 chemicalfan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 88
  • Joined: 16-October 09

Re: SocketServer module + multiprocessing?

Posted 16 July 2010 - 11:24 AM

Ugh, that sounds like it could be a nightmare! I'd rather code two versions, running a "find & replace" to replace fork-related code, for the Thread version. I'd worry about how badly performance would be affected in the Thread version, if several hundred clients were connected to the server (an exaggeration at present, but I'm coding this for scalability), bearing in mind that it's nearly impossible to find a single-core PC nowadays.

I'm not quite settled on SocketServer, but it does seem like the easiest networking API/module to use. I like things simple :)
Was This Post Helpful? 0
  • +
  • -

#6 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: SocketServer module + multiprocessing?

Posted 16 July 2010 - 11:31 AM

View Postchemicalfan, on 16 July 2010 - 12:24 PM, said:

Ugh, that sounds like it could be a nightmare! I'd rather code two versions, running a "find & replace" to replace fork-related code, for the Thread version. I'd worry about how badly performance would be affected in the Thread version, if several hundred clients were connected to the server (an exaggeration at present, but I'm coding this for scalability), bearing in mind that it's nearly impossible to find a single-core PC nowadays.

I'm not quite settled on SocketServer, but it does seem like the easiest networking API/module to use. I like things simple :)


Supposedly, the two mixins are almost identical in their usage; you could probably do something simple like this:

import platform
if platform.system() == 'Windows':
    print("Reverting to Threading, you may have experience poor performance on servers that don't support Forking.")
    class TCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
        pass
else:
    class TCPServer(SocketServer.ForkingMixIn, SocketServer.TCPServer):
        pass

...
# Code use for both should be identical.



Edit: Sorry, I hit submit prematurely.

This post has been edited by Motoma: 16 July 2010 - 11:37 AM

Was This Post Helpful? 1
  • +
  • -

#7 chemicalfan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 88
  • Joined: 16-October 09

Re: SocketServer module + multiprocessing?

Posted 17 July 2010 - 07:38 AM

Thanks for the reply mate, I'll have a look over the code of the two mixins, to make sure the methods & attributes are the same. I'm at the database design & build stage at the moment, so I'll leave the server app for a few days. I'm sure I'll be back :)
Was This Post Helpful? 0
  • +
  • -

#8 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: SocketServer module + multiprocessing?

Posted 17 July 2010 - 08:35 AM

Cool cool. Post back and let me know what you found.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1