News for hackers. Stuff that matters.
Re:Python: Uncovering the Overlooked Core Functionalities by TrianguloY on Monday July 24, 2023 @05:37PM (#36854561)
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
Re:Python: Uncovering the Overlooked Core Functionalities by slt2021 on Monday July 24, 2023 @05:40PM (#36854595)
so Thread based based pool will have same instance of generator, while Process based pool with have unique instance of generator?
Re:Python: Uncovering the Overlooked Core Functionalities by TrianguloY on Monday July 24, 2023 @05:54PM (#36854775)
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!

Re:Python: Uncovering the Overlooked Core Functionalities by kzrdude on Tuesday July 25, 2023 @07:29AM (#36860754)
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.