Your SlideShare is downloading. ×
0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
python-message-0.1.0
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

python-message-0.1.0

3,261

Published on

python-message(v0.1.0) overview.

python-message(v0.1.0) overview.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,261
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
43
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. python-message a message-oriented programming library Lai Yonghao http://laiyonghao.com 2010.12.19
  • 2. outline 2 technical background 4 examples 3 features and APIs 1 brief introduction 5 implementation
  • 3. outline 2 technical background 4 examples 3 features and APIs 1 brief introduction 5 implementation
  • 4. python-message is ... <ul><li>a p ython p ublish- s ubscribe broker for messages within an application (NOT network) . </li></ul><ul><li>inspired by falcon language </li></ul><ul><ul><li>subscribe(&quot;printl&quot;, {tbp => >tbp}) </li></ul></ul><ul><ul><li>broadcast(&quot;printl&quot;, &quot;Hello world!&quot;) </li></ul></ul>
  • 5. home <ul><li>http://pypi.python.org/pypi/message </li></ul><ul><li>http://code.google.com/p/python-message/ </li></ul>
  • 6. installation <ul><li>easy_install message </li></ul>
  • 7. usage <ul><li>import message </li></ul><ul><li>def hello( context, name): </li></ul><ul><li>print 'hello, %s.'%name </li></ul><ul><li>message.sub('greet', hello) </li></ul><ul><li>message.pub('greet', 'lai') </li></ul>
  • 8. outline 2 technical background 4 examples 3 features and APIs 1 brief introduction 5 implementation
  • 9. message-oriented programming <ul><li>Message - oriented programming (MOP) consists in writing program sections generating and replying to messages (happening now, in the future or even happened in the past) instead of writing direct calls. </li></ul><ul><li>A message is formed by a topic and zero or more parameters. </li></ul><ul><li>from Falcon wiki </li></ul>
  • 10. publish-subscribe pattern <ul><li>Publish/subscribe (or pub/sub) is a messaging pattern where senders (publishers) of messages do not program the messages to be sent directly to specific receivers (subscribers). </li></ul><ul><li>This decoupling of publishers and subscribers can allow for greater scalability . </li></ul><ul><li>The observer pattern is a subset of the publish-subscribe pattern. </li></ul><ul><li>from wikipedia </li></ul>
  • 11. outline 2 technical background 4 examples 3 features and APIs 1 brief introduction 5 implementation
  • 12. features <ul><li>the topic is NOT exclusively a string, all Hashable item can be used </li></ul><ul><li>pub() is performed synchronously, the order by which handlers are called is the same order in which they have subscribed </li></ul><ul><li>set context.discontinued = True in listerner function to interrupte publish </li></ul><ul><li>listeners can unsubscribe from listening messages and declarations through the unsub() function </li></ul>
  • 13. APIs <ul><li>sub/unsub </li></ul><ul><li>pub </li></ul><ul><li>declare/retract </li></ul><ul><li>get_declarations/has_declaration </li></ul>
  • 14. sub/unsub <ul><li>sub(topic, listener, front = False) </li></ul><ul><li>unsub(topic, listener) </li></ul><ul><ul><li>topic :Hashable </li></ul></ul><ul><ul><li>listener :Callable, def listener(context, *a, **k):pass </li></ul></ul><ul><ul><li>front :put listener on top of the listeners list, default is False </li></ul></ul>
  • 15. pub <ul><li>pub(topic, *a, **k) </li></ul><ul><ul><li>topic :Hashable </li></ul></ul><ul><ul><li>a and k :parameters will be posted to the listener(s) </li></ul></ul>
  • 16. declare/retract, etc. <ul><li>declare(topic, *a, *k) </li></ul><ul><ul><li>topic :Hashable </li></ul></ul><ul><ul><li>announce topic </li></ul></ul><ul><ul><li>calls all listener s </li></ul></ul><ul><ul><li>if a topic is declared, sub(topic, listener) invokes listener immediately </li></ul></ul><ul><li>retract(topic) </li></ul><ul><ul><li>removes an existing declaration </li></ul></ul><ul><li>get_declarations() </li></ul><ul><li>has_declaration(topic) </li></ul>
  • 17. outline 2 technical background 4 examples 3 features and APIs 1 brief introduction 5 implementation
  • 18. examples <ul><li>decoupling logging from your library </li></ul><ul><li>dancing with process/thread/coroutine </li></ul>
  • 19. decoupling logging from your library(1) <ul><li># before </li></ul><ul><li># foo.py </li></ul><ul><li>logger = logging.getLogger(&quot;prj A &quot;) </li></ul><ul><li>def bar(): </li></ul><ul><li>logger.debug(txt) </li></ul><ul><li>do_sth() </li></ul>
  • 20. decoupling logging from your library(2) <ul><li># after </li></ul><ul><li>import message </li></ul><ul><li>LOG_MSG = ('log', 'foo') </li></ul><ul><li>def bar(): </li></ul><ul><li>messeage.pub(LOG_MSG, 'Haha, Calling bar().') </li></ul><ul><li>do_sth() </li></ul>
  • 21. decoupling logging from your library(3) <ul><li>import logging </li></ul><ul><li>logger = logging.getLogger(&quot;prj A &quot;) </li></ul><ul><li>def handle_foo_log_msg( ctx, txt): </li></ul><ul><li>logger.debug(txt) </li></ul>
  • 22. decoupling logging from your library(4) <ul><li>def handle_foo_log_msg( ctx, txt): </li></ul><ul><li>import logging </li></ul><ul><li>logging.debug(txt) </li></ul>
  • 23. decoupling logging from your library(5) <ul><li>import message </li></ul><ul><li>import foo </li></ul><ul><li>def handle_foo_log_msg( ctx, txt): </li></ul><ul><li>print txt </li></ul><ul><li>message.sub(foo.LOG_MSG, handle_foo_log_msg) </li></ul>
  • 24. dancing with process/thread/coroutine <ul><li>all you need is a decorate function </li></ul><ul><li>@new_XXX </li></ul><ul><li>def listener(context, *a, **k): </li></ul><ul><li>do_sth() </li></ul>
  • 25. dancing with process <ul><li>from multiprocessing import Process </li></ul><ul><li>from functools import wraps </li></ul><ul><li>def new_process(func): </li></ul><ul><li>@wraps(func) </li></ul><ul><li>def _func(*a, **k): </li></ul><ul><li>p = Process(target = func, args = a, kwargs = k) </li></ul><ul><li>p.start() </li></ul><ul><li>return _func </li></ul>
  • 26. dancing with thread <ul><li>from threading import Thread </li></ul><ul><li>from functools import wraps </li></ul><ul><li>def new_thread(func): </li></ul><ul><li>@wraps(func) </li></ul><ul><li>def _func(*a, **k): </li></ul><ul><li>p = Thread(target = func, args = a, kwargs = k) </li></ul><ul><li>p.start() </li></ul><ul><li>return _func </li></ul>
  • 27. dancing with coroutine(greenlet) <ul><li>from greenlet import greenlet </li></ul><ul><li>from functools import wraps </li></ul><ul><li>def new_greenlet(func): </li></ul><ul><li>@wraps(func) </li></ul><ul><li>def _func(*a, **k): </li></ul><ul><li>p = greenlet(func, *a, **k) </li></ul><ul><li>p.start() </li></ul><ul><li>return _func </li></ul>
  • 28. outline 2 technical background 4 examples 3 features and APIs 1 brief introduction 5 implementation
  • 29. Talk is cheap. Show me the code. Linus Torvalds
  • 30. This is not the end, this is just the beginning.
  • 31. reference <ul><li>http://en.wikipedia.org/wiki/Message_passing </li></ul><ul><li>http://en.wikipedia.org/wiki/Publish/subscribe </li></ul><ul><li>http://en.wikipedia.org/wiki/Observer_pattern </li></ul><ul><li>http://pubsub.sourceforge.net/ </li></ul><ul><li>http://falconpl.org/index.ftd?page_id=sitewiki&prj_id=_falcon_site&sid=wiki&pwid=Survival+Guide&wid=Survival%3AMessage+oriented+programming </li></ul>
  • 32. Thank you ! @laiyonghao

×