上游:
import zmq
import random
import time
context = zmq.Context()
# Socket to send messages on
sender = context.socket(zmq.PUSH)
sender.bind("tcp://*:5557")
print "Press Enter when the workers are ready: "
_ = raw_input()
print "Sending tasks to workers..."
# The first message is "0" and signals start of batch
sender.send('0')
# Initialize random number generator
random.seed()
# Send 100 tasks
total_msec = 0
for task_nbr in range(100):
# Random workload from 1 to 100 msecs
workload = random.randint(1, 100)
total_msec += workload
sender.send(str(workload))
print str(workload)
print "Total expected cost: %s msec" % total_msec
工厂:
import sys
import time
import zmq
context = zmq.Context()
# Socket to receive messages on
receiver = context.socket(zmq.PULL)
receiver.connect("tcp://localhost:5557")
# Socket to send messages to
sender = context.socket(zmq.PUSH)
sender.connect("tcp://localhost:5558")
# Process tasks forever
while True:
s = receiver.recv()
# Simple progress indicator for the viewer
sys.stdout.write(s)
sys.stdout.flush()
#Do the work
time.sleep(int(s)*0.001)
# Send results to sink
sender.send(s)
下游:
import sys
import time
import zmq
import MySQLdb
context = zmq.Context()
# Socket to receive messages on
receiver = context.socket(zmq.PULL)
receiver.bind("tcp://*:5558")
# Wait for start of batch
s = receiver.recv()
# Start our clock now
tstart = time.time()
conn=MySQLdb.connect(host='localhost',user='root',passwd='19881028',port=3306)
cur=conn.cursor()
cur.execute('create database if not exists pymqsql')
conn.select_db('pymqsql')
cur.execute('create table test(id int)')
# Process 100 confirmations
total_msec = 0
values=[]
for task_nbr in range(100):
s = receiver.recv()
#if task_nbr % 10 == 0:
sys.stdout.write(s)
values.append(s)
#else:
#sys.stdout.write(':')
cur.executemany('insert into test values(%s)',values)
conn.commit()
cur.close()
conn.close()
# Calculate and report duration of batch
tend = time.time()
print "Total elapsed time: %d msec" % ((tend-tstart)*1000)