python-message-0.1.0
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

python-message-0.1.0

on

  • 3,709 views

python-message(v0.1.0) overview.

python-message(v0.1.0) overview.

Statistics

Views

Total Views
3,709
Views on SlideShare
2,899
Embed Views
810

Actions

Likes
3
Downloads
41
Comments
0

5 Embeds 810

http://code-opensocial.googleusercontent.com 789
https://code-opensocial.googleusercontent.com 14
http://twitter.com 3
http://static.slidesharecdn.com 3
http://translate.googleusercontent.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

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