Skip to content

Instantly share code, notes, and snippets.

View cablehead's full-sized avatar

Andy Gayton cablehead

View GitHub Profile
@cablehead
cablehead / 1.md
Last active October 16, 2015 19:09
Abandoned coroutines in Vanilla

Ben Bangert raised an excellent point about leaking abandoned coroutines in a recent talk:

https://docs.google.com/presentation/d/1LO_WI3N-3p2Wp9PDWyv5B6EGFZ8XTOTNJ7Hd40WOUHo/edit#slide=id.g70b0035b2_1_168

This gist demonstrates a design decision in Vanilla to attempt to help with this.

The sender, recver ends of pipes are only connected by weakref's which have callback's registered for when the corresponding end is garbage collected. When one end is blocked on a send or a recv, and the other end is abandoned without being closed explicitly the blocked coroutine will receive a vanilla.Abandoned exception. This will also result in a StopIteration exception if you are iterating over a recver.

@cablehead
cablehead / e.sh
Last active August 26, 2015 07:03
$ levee run -e '
local levee = require("levee")
local h = levee.Hub()
local serve = h.http:listen(8000)
for conn in serve do
h:spawn(function()
local req = conn:recv()
local sys = require("levee.sys")
local r, w = sys.os.pipe()
sys.os.write(w, s, #s)
local r, w = sys.fd.pipe()
sys.fd.write(w, s, #s)
struct LeveeMiddle {
struct LeveeEnd* ends[2];
}
struct LeveeEnd {
struct LeveeMiddle middle;
}
local ev = require("ev")
ev.run(function(h)
function echo(conn)
for message in conn do
print("Echo:", message)
end
end
local serve = h.tcp:listen(8000)
import json
import vanilla
h = vanilla.Hub()
users = ['antirez', 'justinrosenthal', 'jverkoey']
done = (
import vanilla
h = vanilla.Hub()
serve = h.http.listen()
client = h.http.connect('http://localhost:%s' % serve.port)
response = client.get('/')
conn = serve.recv() # recvs http connection
import vanilla
h = vanilla.Hub()
def handle_request(request):
print request
request.reply(vanilla.http.Status(200), {}, "Hello")
lines = h.pipe()
@h.spawn
def _():
while True:
line = child.stdout.recv_partion('\n')
lines.send(line)
lines.pipe(out)
import vanilla
h = vanilla.Hub()
b = h.bean(8080)
@b.websocket('/')
def _(ws):
child = h.process.execv(['/usr/bin/env', 'grep', '--line-buffered', 'foo'])
child.stdout.pipe(ws)