Skip to content

Instantly share code, notes, and snippets.

@reversefold
Created February 16, 2012 03:32
Show Gist options
  • Save reversefold/53405d856137175aa59d to your computer and use it in GitHub Desktop.
Save reversefold/53405d856137175aa59d to your computer and use it in GitHub Desktop.
Multiprocessing shared data
#!/usr/bin/python
import ctypes
import multiprocessing
import random
import resource
import time
a = None
class Record(ctypes.Structure):
_fields_ = [('value', ctypes.c_wchar_p)]
def __init__(self, value):
self.value = value
def __str__(self):
return '(%s)' % (self.value,)
def child(i):
while True:
print "%ik memory used in child %i: %s" % (resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024, i, a[i])
time.sleep(1)
for j in xrange(len(a)):
c = a[j]
def main():
global a
#a = multiprocessing.Array(ctypes.c_wchar_p, [u'unicode %r!' % i for i in xrange(1000000)], lock=False)
# Using c_wchar_p results in this output:
# 363224k memory used in parent: unicode 0!
# 72560k memory used in child 5: unicode 5!
# 72556k memory used in child 3: unicode 3!
# 72536k memory used in child 1: unicode 1!
# 72568k memory used in child 4: unicode 4!
# 72576k memory used in child 2: unicode 2!
a = multiprocessing.Array(Record, [Record(u'unicode %r!' % i) for i in xrange(1000000)], lock=False)
# Using Record results in this output:
# 775892k memory used in parent: (key 0, unicode 0!)
# 1936k memory used in child 5: (key 5, unicode 5!)
# 1944k memory used in child 3: (key 3, unicode 3!)
# 1940k memory used in child 4: (key 4, unicode 4!)
# 1936k memory used in child 2: (key 2, unicode 2!)
# 1948k memory used in child 1: (key 1, unicode 1!)
for i in xrange(5):
p = multiprocessing.Process(target=child, args=(i + 1,))
p.start()
while True:
print "%ik memory used in parent: %s" % (resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024, a[0])
time.sleep(1)
# for j in xrange(len(a)):
# c = a[j]
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment