share dictionary between processes

Discussion in 'Python' started by blumenkraft, Dec 18, 2009.

  1. blumenkraft

    blumenkraft Guest

    Hi,

    I want to share dictionary between two distinct processes.


    Something like this:

    first.py
    import magic_share_module

    def create_dictionary():
    return {"a": 1}

    magic_share_module.share("shared_dictionary",
    creator.create_dictionary)
    while True:
    pass


    second.py
    import magic_share_module
    d = magic_share_module.get_shared("shared_dictionary")
    print d["a"]

    And then run in command line:
    python first.py &
    sleep 1
    python second.py

    I have looked at POSH, but it requires master process that will fork
    childs. I want read-only sharing between completely unrelated
    processes.
    Is it possible?
     
    blumenkraft, Dec 18, 2009
    #1
    1. Advertising

  2. blumenkraft

    Guest

    blumenkraft <> wrote:
    > Hi,
    >
    > I want to share dictionary between two distinct processes.
    >

    ....
    > I have looked at POSH, but it requires master process that will fork
    > childs. I want read-only sharing between completely unrelated
    > processes.
    > Is it possible?


    Depends on your exact needs - dbm or shelve might be enough, especially for
    read only access.

    --
    -----------------------------------------------------------
    | Radovan Garabík http://kassiopeia.juls.savba.sk/~garabik/ |
    | __..--^^^--..__ garabik @ kassiopeia.juls.savba.sk |
    -----------------------------------------------------------
    Antivirus alert: file .signature infected by signature virus.
    Hi! I'm a signature virus! Copy me into your signature file to help me spread!
     
    , Dec 18, 2009
    #2
    1. Advertising

  3. On Dec 18, 8:48 am, blumenkraft <> wrote:
    > Hi,
    >
    > I want to share dictionary between two distinct processes.
    >
    > Something like this:
    >
    > first.py
    > import magic_share_module
    >
    > def create_dictionary():
    >     return {"a": 1}
    >
    > magic_share_module.share("shared_dictionary",
    > creator.create_dictionary)
    > while True:
    >      pass
    >
    > second.py
    > import magic_share_module
    > d = magic_share_module.get_shared("shared_dictionary")
    > print d["a"]
    >
    > And then run in command line:
    > python first.py &
    > sleep 1
    > python second.py
    >
    > I have looked at POSH, but it requires master process that will fork
    > childs. I want read-only sharing between completely unrelated
    > processes.
    > Is it possible?


    Yes, see http://docs.python.org/library/multiprocessing.html
     
    Michele Simionato, Dec 18, 2009
    #3
  4. blumenkraft

    News123 Guest

    Hi Michael,

    I'm new to the module multiprocessing, but at a first glance
    it seems, that multiprocessing.Value can only be shared if
    you create the second process from the first one.

    Id like to start the first process from the command line and much later
    the second process from the command line.

    Is this possible?


    thanks in advance and bye




    N




    Michele Simionato wrote:
    > On Dec 18, 8:48 am, blumenkraft <> wrote:
    >> Hi,
    >>
    >> I want to share dictionary between two distinct processes.
    >>
    >> Something like this:
    >>
    >> first.py
    >> import magic_share_module
    >>
    >> def create_dictionary():
    >> return {"a": 1}
    >>
    >> magic_share_module.share("shared_dictionary",
    >> creator.create_dictionary)
    >> while True:
    >> pass
    >>
    >> second.py
    >> import magic_share_module
    >> d = magic_share_module.get_shared("shared_dictionary")
    >> print d["a"]
    >>
    >> And then run in command line:
    >> python first.py &
    >> sleep 1
    >> python second.py
    >>
    >> I have looked at POSH, but it requires master process that will fork
    >> childs. I want read-only sharing between completely unrelated
    >> processes.
    >> Is it possible?

    >
    > Yes, see http://docs.python.org/library/multiprocessing.html
     
    News123, Dec 18, 2009
    #4
  5. blumenkraft

    r0g Guest

    blumenkraft wrote:
    > Hi,
    >
    > I want to share dictionary between two distinct processes.
    >
    >
    > Something like this:
    >
    > first.py
    > import magic_share_module
    >
    > def create_dictionary():
    > return {"a": 1}
    >
    > magic_share_module.share("shared_dictionary",
    > creator.create_dictionary)
    > while True:
    > pass
    >
    >
    > second.py
    > import magic_share_module
    > d = magic_share_module.get_shared("shared_dictionary")
    > print d["a"]
    >
    > And then run in command line:
    > python first.py &
    > sleep 1
    > python second.py
    >
    > I have looked at POSH, but it requires master process that will fork
    > childs. I want read-only sharing between completely unrelated
    > processes.
    > Is it possible?




    Depends if you need to have access to the object itself or merely look
    up values in it. If it's the latter you could start a thread in first.py
    that offers dictionary lookups via a specific UDP socket. Any number of
    subsequent progs and processes could then pitch up and lookup whatever
    values they like whenever they like. ~10 lines of code in first.py,
    maybe 5 in second.py

    Roger.
     
    r0g, Dec 18, 2009
    #5
  6. On Thu, Dec 17, 2009 at 23:48 -0800, blumenkraft wrote:
    > I want to share dictionary between two distinct processes.
    > Something like this:
    >
    > first.py
    > import magic_share_module
    > def create_dictionary():
    > return {"a": 1}
    >
    > magic_share_module.share("shared_dictionary",
    > creator.create_dictionary)
    > while True:
    > pass


    Have a look at multiprocessing.Manager() it provides (among other
    things) proxies for dictionaries that can be used in different threads.
    These are even accessible on different hosts if configures correctly.
    --
    .''`. Wolodja Wentland <-heidelberg.de>
    : :' :
    `. `'` 4096R/CAF14EFC
    `- 081C B7CD FF04 2BA9 94EA 36B2 8B7F 7D30 CAF1 4EFC

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.10 (GNU/Linux)

    iQIcBAEBCAAGBQJLK4+6AAoJEIt/fTDK8U78Zq0P/25SC4TFoNJF6dH9KpIpYjwJ
    5Eb0f5sNmqyV8Q1Zs0RYuFTVapPKgDh1IyJXSI4I/LHAwRvnc9zERmixrj60aE5k
    kxMnd1hb1dtVKf4smUd6Ss1Id5fAqIO9PiCsFANDkSLhd5ZqfDh9N+CtbpLB+S3a
    oPJD3wSKvHK5b3aq2cHvlGeFGfbC+TTcEQZHPgPhn2sjZKrIMVIDuDQX5xPeqLj8
    4kTH7+CeqkyLxH8ZO9MsoMVlQgoQLi734chTbyoHV7SJcRdVtrm64ewyv9qHjH/U
    0+OwrXCXcuzbV/ebAqF2G6A6eh2gHh4Zgi2PqB08oti4mGWV/E9RQ6E7CpWKyDQP
    mFJtUzlHYjwXaaUDQY6IOD5LTvJavr+f85toT3aLTelJxKUejzMSsr3i+V2HIuLb
    GBLKezCdR+9bU03RKIWko8htBba8WkFbzGMtrXyfScsN18w9GV5SvAQ68JEa7l6Y
    tnr53FtP28P9xv41w95mTBjSst/tFSLl41WNPmBqTbGvzsdxTQV78pmIU8o86bJ2
    mv6LSbDagpgcKpfASZJwfOff+L2Q51l4Q579vB/+bqnqvSpNI1s16+TIt0qI2zJD
    arKE0H8yIw0JQoKotjB5NOIRgzAXzI/7KjkvhNTD/zR6O3Bp9wxk2vWkuAaC3MrG
    6pKaAlo2/NIkAOC/EC7p
    =HBj7
    -----END PGP SIGNATURE-----
     
    Wolodja Wentland, Dec 18, 2009
    #6
  7. blumenkraft

    Steve Holden Guest

    blumenkraft wrote:
    > Hi,
    >
    > I want to share dictionary between two distinct processes.
    >
    >
    > Something like this:
    >
    > first.py
    > import magic_share_module
    >
    > def create_dictionary():
    > return {"a": 1}
    >
    > magic_share_module.share("shared_dictionary",
    > creator.create_dictionary)
    > while True:
    > pass
    >
    >
    > second.py
    > import magic_share_module
    > d = magic_share_module.get_shared("shared_dictionary")
    > print d["a"]
    >
    > And then run in command line:
    > python first.py &
    > sleep 1
    > python second.py
    >
    > I have looked at POSH, but it requires master process that will fork
    > childs. I want read-only sharing between completely unrelated
    > processes.
    > Is it possible?


    Take a look at pyro, though it may be overkill for your needs.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
    UPCOMING EVENTS: http://holdenweb.eventbrite.com/
     
    Steve Holden, Dec 18, 2009
    #7
  8. blumenkraft

    Steve Holden Guest

    blumenkraft wrote:
    > Hi,
    >
    > I want to share dictionary between two distinct processes.
    >
    >
    > Something like this:
    >
    > first.py
    > import magic_share_module
    >
    > def create_dictionary():
    > return {"a": 1}
    >
    > magic_share_module.share("shared_dictionary",
    > creator.create_dictionary)
    > while True:
    > pass
    >
    >
    > second.py
    > import magic_share_module
    > d = magic_share_module.get_shared("shared_dictionary")
    > print d["a"]
    >
    > And then run in command line:
    > python first.py &
    > sleep 1
    > python second.py
    >
    > I have looked at POSH, but it requires master process that will fork
    > childs. I want read-only sharing between completely unrelated
    > processes.
    > Is it possible?


    Take a look at pyro, though it may be overkill for your needs.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
    UPCOMING EVENTS: http://holdenweb.eventbrite.com/
     
    Steve Holden, Dec 18, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Rodriguez
    Replies:
    23
    Views:
    1,203
    David Schwartz
    Dec 9, 2003
  2. K. Jansma
    Replies:
    7
    Views:
    338
    Duncan Booth
    Feb 21, 2006
  3. Saraswati lakki
    Replies:
    0
    Views:
    1,416
    Saraswati lakki
    Jan 6, 2012
  4. Marc Heiler
    Replies:
    1
    Views:
    191
    Robert Klemme
    May 24, 2009
  5. Replies:
    3
    Views:
    185
Loading...

Share This Page