CodyKochmann
1/18/2018 - 1:35 PM

This demonstrates the impact of serializing the data going through pythons multiprocessing.Queue has on speed.

This demonstrates the impact of serializing the data going through pythons multiprocessing.Queue has on speed.

from multiprocessing import Queue

from generators import rps
from itertools import cycle

'''
  This demonstrates the impact of serializing the data going
  through pythons multiprocessing.Queue has on speed.
  
  by: Cody Kochmann
'''

# test the speed of queue throughput using serialization
def with_serialization():
    q = Queue()
    infinite_strings = cycle(list('1234567890'))
    for s in infinite_strings:
        q.put(s)
        yield q.get()

print(rps(with_serialization()))
print(rps(with_serialization()))
print(rps(with_serialization()))
# 8116
# 9582
# 9645

# now, we are gonna turn off serialization and assume all inputs will be strings

# create a Queue to get access to the _feed function
q = Queue()
# replace the serialization methods used by multiprocessing's internals
q._feed.__globals__['_ForkingPickler'].dumps = str.encode
q._feed.__globals__['_ForkingPickler'].dump = lambda obj, file, *a, **k: file.write(str.encode(i))
q._feed.__globals__['_ForkingPickler'].loads = bytes.decode
# do a little cleanup since this was just being used as an access point
q.close()
del q

# multiprocessing now does not use serialization on its inputs

# now test the speed without serialization
def without_serialization():
    q = Queue()
    infinite_strings = cycle(list('1234567890'))
    for s in infinite_strings:
        q.put(s)
        yield q.get()

print(rps(without_serialization()))
print(rps(without_serialization()))
print(rps(without_serialization()))
# 11211
# 11256
# 11271