Skip to content Skip to sidebar Skip to footer

How Can Processes Share List Between Each Other In Python?

Please be so kind to point me on how can a list or an array be shared between processes so they can access/append/delete data from it? Do i need to use Manager for it? For example

Solution 1:

The data structure you're looking for is multiprocessing.Queue. You can pop values out of the queue into a list until you've got as many values as there were processes, then sort and print them. With your particular application, however, Dan D's pool.map answer has a better approach.

Solution 2:

note that pool.map acts a lot like the buildin map function and like it returns a list of the results of the application of the given function to elements in the given list.

so you need only have ping() return what it found and then do:

list_of_values = pool.map(ping, host, 1)

after which you can use the list_of_values any way you like

Solution 3:

I strongly recommend that you use something like redis ( http://redis.io ), which is a datastructures server - it exists to facilitate the sharing of datastructures in a robust manner.

Post a Comment for "How Can Processes Share List Between Each Other In Python?"