Python Forum
generator concurrency - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: generator concurrency (/thread-14935.html)



generator concurrency - Skaperen - Dec-25-2018

so i wrote a generator. it's not a class. it's just a function with a yield statement. calling it returns an iterator that effectively drives the generator.

what if i call the generator twice and get 2 iterators? can i still use either? can i use the other before the one i use first is complete? can i interleave them?


RE: generator concurrency - Gribouillis - Dec-25-2018

Both instances are independent. Each has its local namespace. You can use each of them without worrying about the other.


RE: generator concurrency - DeaD_EyE - Dec-25-2018

If I have to use my own words:

A generator is returned by calling a function when a yield statement is inside the function.
Each instance of a generator has it's own context.

Yes, they are independent.


RE: generator concurrency - Skaperen - Dec-26-2018

this is what i wanted to know. i could run a test, but i was not sure my test would truly show a lack of independence, if that was the case.

my use case is comparing two directories to find extra or missing members using my file tree recursion generator (recently revised).

def ftrgen(path,**opts):
    """File tree recursion (ftr) that yields all paths."""
    from os import listdir,readlink
    from sys import argv,stdout
    pre='TypeError: '+__name__+'() '
    depth = opts.pop('depth',2**62)
    if opts:
        raise TypeError(pre+'got an unexpected keyword argument '+' '.join([repr(x) for x in opts.keys()]))
    if not isinstance(path,str):
        raise TypeError(pre+'apex path is not a string')
    level = [[path]]
    deep = 0
    while level[0]:
        if level[-1]:
            path = '/'.join([x[0] for x in level])
            yield path
            try:
                readlink(path)
                names = []
            except OSError:
                try:
                    names = sorted(listdir(path))
                except OSError:
                    names = []
                deep += 1
                if deep > depth:
                    names = []
            if names:
                level.append(names)
            else:
                level[-1].pop(0)
            continue
        else:
            level[-1:] = []
            level[-1].pop(0)

according to documentation, generators save local namespace. nothing is said about global namespace. if i am iterating two "copies" of the same generator, where "same" means is that they are imported from the same file, which experience tells me they share the global space (which a function, such as a generator, should not save state in).