Created
October 5, 2019 19:47
-
-
Save jtallieu/d24cf7ac9574f433336ddff75279d893 to your computer and use it in GitHub Desktop.
Passing Foo instance over a socket.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gevent | |
from gevent import monkey | |
monkey.patch_all() | |
import os | |
os.environ['MONKEY'] = "True" | |
import pickle | |
import socket | |
r, w = socket.socketpair() | |
class Foo(object): | |
def __init__(self, name): | |
self.name = name | |
def writer(): | |
write = w.makefile("wb", 100) | |
for i in range(0, 10): | |
# object to pickle and send over | |
array = [1, 2, "hello", {'age': i}, Foo("foobar")] | |
pickle.dump(array, write) | |
gevent.sleep(.5) | |
w.close() | |
print "Done writing" | |
def reader(): | |
rr = r.makefile("rb") | |
chars = None | |
first = True | |
while first or chars: | |
chars = pickle.load(rr) | |
print "READ {}".format(chars) | |
gevent.sleep(2.5) | |
greenlets = [] | |
greenlets.append(gevent.spawn(writer)) | |
greenlets.append(gevent.spawn(reader)) | |
gevent.wait(greenlets) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment