param icon indicating copy to clipboard operation
param copied to clipboard

generators not working

Open majidaldo opened this issue 9 months ago • 7 comments

ALL software version info

Software Version Info
python 3.11.9
param 2.2.0

Description of expected behavior and the observed behavior

should print g

Complete, minimal, self-contained example code that reproduces the issue

def g():
    while True: yield 'g'

import param
class P(param.Parameterized):
    p = param.String(allow_refs=True)
p = P(p=g)
#print(next(g()))
print(p.p)  # nothing prints

majidaldo avatar Mar 27 '25 17:03 majidaldo

I can reproduce this. It works a bit better in a notebook, if the last line is executed in another cell.

maximlt avatar Apr 02 '25 07:04 maximlt

This doesn't seem like a bug to me, the generator has to get scheduled on a thread so it doesn't block things on the main thread so if you print immediately after running it, it won't have time to run and yield a value.

philippjfr avatar Apr 02 '25 10:04 philippjfr

This doesn't seem like a bug to me, the generator has to get scheduled on a thread so it doesn't block things on the main thread so if you print immediately after running it, it won't have time to run and yield a value.

sigh. that would not be expected behavior.

majidaldo avatar Apr 02 '25 15:04 majidaldo

This doesn't seem like a bug to me, the generator has to get scheduled on a thread so it doesn't block things on the main thread so if you print immediately after running it, it won't have time to run and yield a value.

Ok indeed that's at least documented there https://param.holoviz.org/user_guide/Generators.html#asynchronous-generators. I also find that surprising to be honest.

maximlt avatar Apr 03 '25 10:04 maximlt

There's really no alternative, a generator like this:

def g():
    while True: yield 'g'

would simply block forever.

philippjfr avatar Apr 03 '25 10:04 philippjfr

Maybe we should start with your use case, what would be expected behavior for you here? Yielding from the generator once, yielding from it every time the parameter is accessed, yielding from it when a class is instantiated?

philippjfr avatar Apr 03 '25 10:04 philippjfr

Maybe we should start with your use case, what would be expected behavior for you here? Yielding from the generator once, yielding from it every time the parameter is accessed, yielding from it when a class is instantiated?

I was expecting the getting behavior to simply like calling next on the generator : on access and on inst. Perhaps param should deal with functions (as blocking) and generators (as non-blocking).

majidaldo avatar Apr 07 '25 20:04 majidaldo