9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: roger peppe <rogpeppe@gmail.com>
To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net>
Subject: Re: [9fans] python csp
Date: Thu, 12 Mar 2009 19:51:10 +0000	[thread overview]
Message-ID: <df49a7370903121251u39a3ad05h3d530cabc33e2959@mail.gmail.com> (raw)
In-Reply-To: <4f34febc0903120959r1fd5746ev2e6e9fb068958fb0@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 875 bytes --]

2009/3/12 John Barham <jbarham@gmail.com>:
>>> How about using queues (http://docs.python.org/library/queue.html)?
>>
>> no alt.
>
> Couldn't you implement it approximately using
> http://docs.python.org/library/queue.html#Queue.Queue.qsize?

no. "approximately" in this case would mean "wrong".

for the time being, as i'm limited on time, i'm using pycsp
with a very slim wrapper around it to make its interface
palatable. it'll all be hideously inefficient, but i'm not
too concerned about that currently. i've attached it in case
anyone's interested.

joel: multiple readers and writers isn't the same thing as alt
at all - you need to be able to read from more than
one channel at once. sharing a single channel between
several processes doesn't get you that (although it's
great to have, and the plan9/inferno channel model
supports it)

  rog.

[-- Attachment #2: p9csp.py --]
[-- Type: application/octet-stream, Size: 1213 bytes --]

import pycsp

Channel = pycsp.Channel

def chan(*n):
	'''Create a channel. If an argument is given, it specifies
	the buffer size (default 0, unbuffered)'''
	if len(n) == 0:
		n = 0
	else:
		n = n[0]
	if n > 0:
		c = pycsp.BufferedAny2OneChannel(buffer = pycsp.FifoBuffer(n))
	else:
		c = pycsp.Any2OneChannel();
	return c;

def alt(*cs):
	'''alt(chan....)
	Alt waits until one of the channels becomes ready for reading,
	then returns its read member - it is up to the caller
	to do the read'''
	return pycsp.Alternative(*map(lambda c: c.read, cs)).select();

def altf(*cs):
	'''altf(chan..., fn, chan..., fn, ...)
	Altf waits until one of the channels is ready for reading,
	then reads from it, calls its associated function,
	and returns the result of that call.'''

	as = []
	fs = []
	i = 0
	while i < len(cs):
		j = i
		while isinstance(cs[j], Channel):
			j = j + 1
		if i == j:
			raise 'expected channel(s)'
		f = cs[j]
		k = i
		while k < j:
			as.append(cs[k].read)
			fs.append(f)
			k = k + 1
		i = j + 1
	a = pycsp.Alternative(*as)
	s = a.select()
	i = 0
	while i < len(as):
		if s == as[i]:
			return fs[i](s())
		i = i + 1

def spawn(f, *args, **kwargs):
	pycsp.Process(f, *args, **kwargs).start();

  reply	other threads:[~2009-03-12 19:51 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-11 16:58 roger peppe
2009-03-11 17:13 ` gdiaz
2009-03-11 19:10   ` Uriel
2009-03-11 19:34     ` J.R. Mauro
2009-03-12  8:08   ` roger peppe
2009-03-12 15:44     ` John Barham
2009-03-12 16:10       ` roger peppe
2009-03-12 16:59         ` John Barham
2009-03-12 19:51           ` roger peppe [this message]
2009-03-12 15:48     ` andrey mirtchovski
2009-03-12 17:54     ` Joel C. Salomon
2009-03-12 18:06       ` David Leimbach

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=df49a7370903121251u39a3ad05h3d530cabc33e2959@mail.gmail.com \
    --to=rogpeppe@gmail.com \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).