Hacker News new | ask | show | jobs
by TrianguloY 1057 days ago
A yield will simply return a generator object, which contains information about the next value to use, and how to continue the function execution. That's why you need to use functions that yield things inside loops or list(...).

If you run it from different threads I guess it will be the same as calling the function multiple times, it will return a new started-from-the-top generator.

    def sum():
        yield 1
        yield 2
    print(repr(sum()))
    print(next(sum()))
    print(next(sum()))
Prints

    <generator object sum at 0x7fc6f14823c0>
    1
    1
1 comments

so Thread based based pool will have same instance of generator, while Process based pool with have unique instance of generator?
In this example, calling sum() creates a generator and returns it. Say g = sum(). If you share g between threads, they will all use the same generator object! If you call sum() separately per thread, they will be different generators.

If you try to send g to a different process, you will get an error, because it doesn't serialize.

I don't know if a generator can be shared across threads, but in that case ... I have no idea :/

You'll need to search, or try!