
Да вземем една опашка

Не точно:

По скоро:

1 class Queue(object):
2 def __init__(self):
3 self.lock = Lock()
4 self.queue = []
5
6 def pop(self):
7 with self.lock:
8 return self.queue.pop(0)
9
10 def push(self, v):
11 with self.lock:
12 self.queue.append(v)


430 стр.
Прекалено ниско ниво на абстракция.
Подобно на GC.
Не е нужно всеки път да откриваме топлата вода.



Actor model
1 -module(counter).
2 -export([run/0, counter/1]).
3
4 run() ->
5 S = spawn(counter, counter, [0]),
6 send_msgs(S, 100000),
7 S.
8
9 counter(Sum) ->
10 receive
11 value -> io:fwrite("Value is ~w~n", [Sum]);
12 {inc, Amount} -> counter(Sum+Amount)
13 end.
14
15 send_msgs(_, 0) -> true;
16 send_msgs(S, Count) ->
17 S ! {inc, 1},
18 send_msgs(S, Count-1).




Map-Reduce?


Pipeline
1 from pika.adapters import BlockingConnection
2 from pika import BasicProperties
3
4 # Open a connection to RabbitMQ on localhost
5 # using all default parameters
6 connection = BlockingConnection()
7
8 # Open the channel
9 channel = connection.channel()
1 channel.exchange_declare(
2 exchange='fest-exchange',
3 type='direct')
1 channel.queue_declare(
2 queue="fest-queue",
3 durable=True,
4 auto_delete=False)
1 channel.queue_bind(
2 queue='fest-queue',
3 exchange='fest-exchange')



1 byte[] messageBodyBytes = "Hello, world!".getBytes();
2 channel.basicPublish("fest-exchange"", null, null,
3 messageBodyBytes);
1 def handle_delivery(channel,
2 method_frame, header_frame, body):
3 # Process the message
4 log.debug('Got message with data: %s',
5 body)
6
7 channel.basic_consume(
8 handle_delivery, queue='fest-queue')
1 def handle_delivery(channel,
2 method_frame, header_frame, body):
3 # Process the message and acknowledge it
4
5 channel.basic_ack(
6 delivery_tag=method_frame.delivery_tag)
7
8 channel.basic_consume(
9 handle_delivery, queue='fest-queue')
Не бива опашките или съобщенията в тях да стават много.
Едно от най-добрите парчета софтуер, които съм ползвал.
-- Емил Иванов
1 import zmq, datetime, time
2
3 ctx = zmq.Context()
4 socket = ctx.socket(zmq.PUB)
5 socket.bind("tcp://*:5555")
6
7 for i in range(10000):
8 socket.send('Time is %s' % datetime.datetime.now())
9 time.sleep(1)
1 import zmq
2
3 ctx = zmq.Context()
4 socket = ctx.socket(zmq.SUB)
5 socket.connect("tcp://127.0.0.1:5555")
6
7 for i in range(10):
8 print socket.recv()
9
10 # 2011-10-27 12:58:59.015099
11 # 2011-10-27 12:58:60.843325
12 # 2011-10-27 12:58:61.819903
Изпуска съобщенията, ако няма кой да слуша (неможе с RabbitMQ)
1 import org.zeromq.ZMQ
2
3 val c = ZMQ.context(2)
4 val s = c.socket(ZMQ.PUSH)
5 s.bind("tcp://*:5556")
6 1 to 10 foreach {i =>
7 s.send("Hello %s".format(i).getBytes(),
8 0) // flags
9 }
Идея за демо
URL fetch - раздаване - фечване - обобщаване
Програмирането с нишки, семафори и други ЖП елементи е трудно. Една от алтернативите е различните процеси комуникират със съобщения. RabbitMQ и ZeroMQ са едни от примерите. Нека трудните проблеми да бъдат решени веднъж за винаги.
Благодаря
Въпроси?
| Table of Contents | t |
|---|---|
| Exposé | ESC |
| Full screen slides | e |
| Presenter View | p |
| Source Files | s |
| Slide Numbers | n |
| Toggle screen blanking | b |
| Show/hide slide context | c |
| Notes | 2 |
| Help | h |