Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Sample Python multiprocessing.connection server

Sample Python multiprocessing.connection server

Sample Python multiprocessing.connection server

For those of you learning multiprocessing and multiprocessing.connection modules, here is a sample server that launches parallel children processes, echoes messages and will stress the CPUs on demand.

BTW, you MUST use the multiprocessing.connection.Client to connect with the server. Raw sockets cause MemoryError, see: http://bugs.python.org/issue16920

CODE --> python

# Written by John Brearley,  Jan 2013

# 2013/1/10 submitted bug to: http://bugs.python.org/issue16920
# Answer: MUST use multiprocessing client! It has msg length prefix in data.

import multiprocessing,multiprocessing.connection,re,sys,threading,time

rc_shutdown=3 ;# return code to signal server shutdown
srv_ctl=1 ;# flag to control while loops

# Routine handles new incoming sockets as a separate process.
def handle_socket(l,q):
   print("handle_socket name:",name,"waiting for socket")
   # print("handle_socket l:",l,"q:",q)
   # print_data.print_data(l)  ;# for learning purposes
   # print_data.print_data(q)

   # code blocks here waiting for incoming socket.
   # print("handle_socket name:",name,"conn:",conn)
   # print_data.print_data(conn) ;# for learning purposes
   # print("handle_socket name:",name,"fileno:",conn.fileno())
   # How to get getsockname() getpeername() ?

   # Tell the server to launch another child process.
   # We dont do it here, as it will become a child of this
   # process, and wont be visible to the server as an independant
   # process. Sub-children process seem to be hidden from the parent.
   q.put("launch") ;# tells the server to launch another process

   # Here conn.poll() does work. However, since this is a server, its job is
   # to sit and wait, so blocking mode is fine. There may be cases where you
   # want to detect the remote client going to sleep, being slow, so you can
   # implement timeout loops using conn.poll(x)
   # rc=conn.poll(0.5)
   # print("handle_socket poll rc:",rc)

   # New incoming socket is in blocking mode, no supported option for non-blocking.
   print("handle_socket name:",name,"waiting for data")
   while (1):
      # Wait for data
         if (msg == ""):
            print("handle_socket recv on name:",name,"null msg ==> socket closing!","msg:",msg)
            print("handle_socket recv on name:",name,"ERROR: ==> socket closing!","msg:",msg)

      # Did we get any data?
      dr=len(data) ;# received data length
      if (dr == 0):
         print("handle_socket recv on name:",name,"no data ==> socket closing!")

      # Send back the received data
      print("handle_socket send on name:",name,"data:",data)
      conn.send(data) ;# Does NOT return data length like base socket does

      # Check for commands in received data
      # NB: str chokes on null data!!!
      # str() will be covered by dr==0 check above.
         print("handle_socket name:",name,"ERROR:",sys.exc_info()[1])
      # print("handle_socket name:",name,"temp:",temp)

      # Look for stress command, with optional number of seconds.
      # Drive CPU to 100% useage. Helps prove that tasks are distributed across available CPU.
      if (m):
            delay=int(m.group(1)) ;# use optional number from stress command
         print("handle_socket creating CPU stress for:",delay,"seconds, cmd from:",name)
         while (temp < delay):
         print("handle_socket done CPU stress for:",delay,"seconds, cmd from:",name)

      # Look for shutdown command.
      if (temp == "shutdown"):
         print("handle_socket Shutting down server, cmd from:",name)
         rc=rc_shutdown ;# Main server loop looks at exitcode of child process

   # Close socket, give appropriate return code.

def monitor_stdin():
   global srv_ctl

   # Main process shows sys.stdin is connected to stdin. BUT, a child
   # process shows null! A thread also has access to stdin.
   # print_data.print_data(sys.stdin)

   # Check keyboard/stdin on terminal window for shutdown command.
   # Done as a separate process as I cant find a way to make stdin
   # non-blocking. On Windows, select only works on sockets, not stdin.
   print("monitor_stdin looking for keyboard input")
   while (srv_ctl):
      line=input() ;# blocking call
      # line=sys.stdin.readline() ;# blocking call
      if (line == ""):
         # Most of the time there will be no stdin data available.
      elif (line == "shutdown"):
         print("monitor_stdin shutting down server, cmd from keyboard")
         srv_ctl=0 ;# Stops main server loop and this local loop
         print("monitor_stdin WARNING: ignoring:",line)
         print("monitor_stdin To shutdown the server process, type: shutdown")

# Main program
if (__name__ == "__main__"):

   # Create a new multiprocessing connection listener object
   host="" ;# ' means use all addresses available
   # print_data.print_data(l) ;# for learning purposes
   # Listener has no methods listen, setblocking or fileno
   print("\nmain server listening on host",host,"port",port)

   # Code is designed around the fact that the listener object is blocking only!
   # method poll() is for Client only, not Listener!
   # rc=s.poll(0.5) ;# test code, fails!
   # print("main poll rc:",rc)

   # The issue with the first child process launching another child process is that
   # the first child process will not terminate untill all its children process have
   # also terminated. Also, the sub-children process do not appear to be visible to
   # the main process. To avoid these issues, we ensure that the main process always
   # launches the child process.

   # Since we only want one child process doing an accept() at any given time, we need
   # the child process to be able to signal the server process when its time to launch
   # another process. This is done using the multiprocessing queue.
   # print_data.print_data(q) ;# for learning purposes
   q.put("launch") ;# tells the server to launch the first process
   # print("main qsize:",q.qsize(),"empty:",q.empty())

   # Keep track of all children launched. While the method multiprocessing.active_children()
   # will give us the currently active children, it wont tell us about children that just terminated.
   # So if we want to check on the exitcode of a recently terminated child, we need to have our
   # own child list that we can go look at.

   # NB: We could use the queue pass back a shutdown command. However, I chose to use
   # child process return codes to signal the shutdown command.

   # We also watch the terminal window keyboard/stdin for the shutdown command.
   # This is done as a seperate thread, as I cant find a non-blocking way to read
   # stdin on Windows. A thread has access to stdin, but a child process does not.
   t=threading.Thread(target=monitor_stdin);# create new process
   # Keep thread object separate, as thread has no exitcode to monitor or terminate method
   t.start() ;# dont use obj.run(), run forces serial mode, not parallel!
   print("main created t.ident:",t.ident,"t.name:",t.name)

   # Main server loop
   while (srv_ctl):

      # Get next item from queue. By default, q.get will wait forever if no work is available!
      # We add a 3 sec timeout to avoid lockup!
      # print("main q task:",task)

      # If necessary, launch a handle_socket with access to listener & queue objects.
      if (task == "launch"):
         # NB: args=(...,) must have at least one comma! Even if only 1 arg!
         obj=multiprocessing.Process(target=handle_socket, args=(l,q));# create new process
         # print("main created process",obj)
         child_list.append(obj) ;# save the child object
         obj.start() ;# dont use obj.run(), run forces serial mode, not parallel!
         # child pid is stored in object as ident & pid, wont have usefull value until process actually starts
         print("main created obj.pid:",obj.pid,"obj.name:",obj.name)

      # Poll existing children processes.
      # print("main child_list:",child_list)
      if (len(child_list) == 0):
         # print("main no active children")
         for i,child in enumerate(child_list):
            # print("main polling i:",i,"child:",child)
            if (not child.is_alive()):
               # child process has finished, get exitcode
               print("main child:",child.name,"done, rc:",rc)
               if (rc != None):
                  # Forget this child, we have now got its exitcode
                  del child_list[i]
                  # Child process return code can signal shutdown for server.
                  if (rc == rc_shutdown):
                     print("main shutting down server, cmd from:",child.name)
                     srv_ctl=0 ;# stops main while loop & monitor_stdin while loop

   # Shutdown server, terminate any remaining children processes.
   for child in child_list:
      if (child.is_alive()):
         print("main child:",child.name,"terminated rc:",child.exitcode)

   # End main program. Prompt user, wait for thread to terminate.
   if (t.is_alive()):
      print("\nmain hit return to end the keyboard monitor thread.")
      while (t.is_alive()):
   print("\nmain all done.")

RE: Sample Python multiprocessing.connection server

Here is the corresponding client code:

CODE --> python

# Updated by John Brearley,  Jan 2013

# multiprocessing.connection client program

# 2013/1/13 poll bug submitted http://bugs.python.org/issue16955

import multiprocessing.connection,re,sys

# Get command line parameters, if any, for desired host & port.
host="localhost" ;# default host
if (argc > 1):
   if (temp != ""):
port=10500 ;# default port
if (argc > 2):
   if (re.search("^\d+$",temp)):
      port=int(temp) ;# int() gives error on null string
# print("argc:",argc,"host:",host,"port:",port)

# Create new client object
print("\nConnecting to host:",host,"port:",port)
# print_data.print_data(cl) ;# for learning purposes

# MUST use default client blocking mode! There is no setblocking() method!

# Show socket info.
# Fileno is unique only to this instance of python.
# Fileno is often reused by other concurrently running python programs.
# print("fileno:",cl.fileno())
# How to do getsockname() getpeername() ?

# Process user input.
while (1):
   # print("cmd:",cmd)
   if (cmd == "q"):

   # Send cmd, wait for response.
   # NB: To force the server to become non-response, in the server window
   # type CTRL-S ("xoff") several times. This allows you to test the error
   # handling code below.
   # NB: sending a null byte string doesnt send anything to server. If you
   # turn off the cmd.strip and the if below, then you can send & receive
   # a single space or more.
   if (cmd != ""):
      timeout=1 ;# error flag

      # The for loop provides a poll/wait timeout for blocking operations.
      for i in range(16):
         # poll(x) waits for x seconds for input to be available before returning.
         # This is how you deal with timeouts for a blocking client object.
            # readable=cl.poll(0.2) ;# broken on Windows, see http://bugs.python.org/issue16955
            readable=bool(multiprocessing.connection.wait([cl],0.2)) ;# Work around for issue 16955
            # print("i:",i,"readable:",readable)

         if (readable):
               # cl.close() ;# test code

            timeout=0 ;# we got some data

      if (timeout):
         print("ERROR4: Timeout for cmd:",cmd)

# Clean up.
print("\nAll done!") 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close