Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a generator and threads
#1
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?
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
Do you mean problems aside from the usual ones with shared mutable state, like race conditions?
Reply
#3
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
(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.
Reply
#5
(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.
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