Python Forum

Full Version: a generator and threads
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
if a generator spawns a thread, what happens? can 2 or more threads do yields in the same generator? what if 2 or more threads try to iterate the same generator?
Do you mean problems aside from the usual ones with shared mutable state, like race conditions?
that and how being a generator might be involved with those conditions. if a generator spawns a thread, it depends on how things are coded to handle the yield. the yield will be expected to block the that did the yield. there will be some kind of internal object to represent the communication state of the yields. can it control the flow from both threads and merge the content? this could be done at the C layer in threads are considered in the design.

iterating the same generator may or may not be pointless. if it can be made to work (it can in C) it could be a good way to spread a work queue to many threads.
(Oct-31-2022, 05:47 PM)Skaperen Wrote: [ -> ]it could be a good way to spread a work queue to many threads.
Your question looks like an instance of a XY problem. The description is very vague: it depends on such and such....

Try to describe a simple problem that you want to solve with threads and why you think using thread-friendly generators is the solution.
(Oct-31-2022, 09:23 PM)Gribouillis Wrote: [ -> ]Try to describe a simple problem that you want to solve with threads and why you think using thread-friendly generators is the solution.
launch a subprocess and feed data to it over a pipe to its stdin while concurrently receiving data from its stdout over another pipe. maybe also receive stderr. a function to do this handles all but stdout by processing data to/from some kind of buffer (list, bytearray) and for the process stdout, this function will yield a line at a time. the caller gets the generator instance to get that output from. i'm developing a function to run processes and am exploring all the ways to handle data and work with callers.