Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
generator concurrency
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Both instances are independent. Each has its local namespace. You can use each of them without worrying about the other.
Reply
#3
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
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).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020