mixing greenlets and processes

61 views
Skip to first unread message

Arthur Edelstein

unread,
Jun 11, 2010, 4:15:01 AM6/11/10
to pycsp
Hello,

Is it possible to mix different pycsp modes together (e.g., two
processes each running several greenlets)? Thanks.

Best regards,
Arthur

Rune M. Friborg

unread,
Jun 11, 2010, 4:31:42 AM6/11/10
to pycsp
Hi Arthur,

I always thought of it as possible, but now when I think about it in
detail, then I foresee a problem when
mixing the thread version with greenlets.

The greenlets version use a global scheduler and since threads share a
common address space,
the greenlets scheduler would fail. Mixing greenlets and processes
should be no problem at all though,
since processes have a private address space.

import pycsp.processes as heavy
import pycsp.greenlets as light
import sys, time

@light.process
def source(chan_out):
for i in range(10):
time.sleep(0.1)
chan_out("Hello world (%d)\n" % (i))
light.retire(chan_out)

@light.process
def sink(chan_in):
while True:
sys.stdout.write(chan_in())

@heavy.process
def P():
chan = light.Channel()
light.Parallel(
source(chan.writer()),
sink(chan.reader())
)

heavy.Parallel(P()*2)



As expected, when replacing threads with processes, this is the
result:

$ python mixing.py
Exception in thread Thread-2:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/threading.py", line 522, in __bootstrap_inner
self.run()
File "../pycsp/threads/process.py", line 81, in run
File "mixing.py", line 46, in P
sink(chan.reader())
File "../pycsp/greenlets/process.py", line 184, in Parallel
File "../pycsp/greenlets/process.py", line 207, in _parallel
File "../pycsp/greenlets/scheduling.py", line 267, in join
error: cannot switch to a different thread

One might consider to add a thread_id to global greenlet scheduler.

Best regards, Rune


On Jun 11, 10:15 am, Arthur Edelstein <arthuredelst...@gmail.com>
wrote:

Rune M. Friborg

unread,
Jun 11, 2010, 4:34:39 AM6/11/10
to pycsp

whoops!

meant:
As expected, when replacing processes with threads, it fails.

End result:

Processes and Greenlets : It works!
Threads and Greenlets : Fails!

- Rune
Reply all
Reply to author
Forward
0 new messages