~ubuntu-branches/ubuntu/wily/pyzmq/wily

« back to all changes in this revision

Viewing changes to examples/gevent/simple.py

  • Committer: Package Import Robot
  • Author(s): Julian Taylor
  • Date: 2013-02-24 19:23:15 UTC
  • mfrom: (1.2.1) (9 sid)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: package-import@ubuntu.com-20130224192315-qhmwp3m3ymk8r60d
Tags: 2.2.0.1-1
* New upstream release
* relicense debian packaging to LGPL-3
* update watch file to use github directly
  thanks to Bart Martens for the file
* add autopkgtests
* drop obsolete DM-Upload-Allowed
* bump standard to 3.9.4, no changes required

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from gevent import spawn, spawn_later
 
2
import zmq.green as zmq
 
3
 
 
4
# server
 
5
print zmq.Context
 
6
ctx = zmq.Context()
 
7
sock = ctx.socket(zmq.PUSH)
 
8
sock.bind('ipc:///tmp/zmqtest')
 
9
 
 
10
spawn(sock.send_pyobj, ('this', 'is', 'a', 'python', 'tuple'))
 
11
spawn_later(1, sock.send_pyobj, {'hi': 1234})
 
12
spawn_later(2, sock.send_pyobj, ({'this': ['is a more complicated object', ':)']}, 42, 42, 42))
 
13
spawn_later(3, sock.send_pyobj, 'foobar')
 
14
spawn_later(4, sock.send_pyobj, 'quit')
 
15
 
 
16
 
 
17
# client
 
18
ctx = zmq.Context() # create a new context to kick the wheels
 
19
sock = ctx.socket(zmq.PULL)
 
20
sock.connect('ipc:///tmp/zmqtest')
 
21
 
 
22
def get_objs(sock):
 
23
    while True:
 
24
        o = sock.recv_pyobj()
 
25
        print 'received python object:', o
 
26
        if o == 'quit':
 
27
            print 'exiting.'
 
28
            break
 
29
 
 
30
def print_every(s, t=None):
 
31
    print s
 
32
    if t:
 
33
        spawn_later(t, print_every, s, t)
 
34
 
 
35
print_every('printing every half second', 0.5)
 
36
spawn(get_objs, sock).join()
 
37