File: pair-fork.py

package info (click to toggle)
pyro4 4.82-2
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 2,528 kB
  • sloc: python: 17,736; makefile: 169; sh: 113; javascript: 62
file content (52 lines) | stat: -rw-r--r-- 1,664 bytes parent folder | download | duplicates (3)
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
# this example forks() and thus won't work on Windows.

from __future__ import print_function
import os
import signal
import socket
import Pyro4

# it's okay to use Pickle because we trust our own processes, and it is efficient
Pyro4.config.SERIALIZER = "pickle"
Pyro4.config.SERIALIZERS_ACCEPTED |= {"pickle"}


# create our own socket pair (server-client sockets that are already connected)
sock1, sock2 = socket.socketpair()

pid = os.fork()

if pid == 0:
    # we are the child process, we host the daemon.

    class Echo(object):
        @Pyro4.expose
        def echo(self, message):
            print("server got message: ", message)
            return "thank you"

    # create a daemon with some Pyro objectrunning on our custom server socket
    daemon = Pyro4.Daemon(connected_socket=sock1)
    daemon.register(Echo, "echo")
    print("Process PID={:d}: Pyro daemon running on {:s}\n".format(os.getpid(), daemon.locationStr))
    daemon.requestLoop()

else:
    # we are the parent process, we create a Pyro client proxy
    print("Process PID={:d}: Pyro client.\n".format(os.getpid()))

    # create a client running on the client socket
    with Pyro4.Proxy("echo", connected_socket=sock2) as p:
        reply = p.echo("hello!")
        print("client got reply:", reply)
        reply = p.echo("hello again!")
        print("client got reply:", reply)
    with Pyro4.Proxy("echo", connected_socket=sock2) as p:
        reply = p.echo("hello2!")
        print("client got reply:", reply)
        reply = p.echo("hello2 again!")
        print("client got reply:", reply)

    os.kill(pid, signal.SIGTERM)
    os.waitpid(pid, 0)
    print("\nThe end.")