Basically the question boils down to wether it is possible to have parts
of a program (could be functions) that keep doing their job while other
parts do something else on the same data, and what is the best way to do >this.
if len( list ):
print( "List contains " + str( list[ 0 ]) + "." )
lock.acquire()
Yes, but this is difficult. If you ask this question here,
you might not be ready for this.
I haven't learned it yet myself, but nevertheless tried to
write a small example program quickly, which might still
contain errors because of my lack of education.
import threading
import time
def write_to_list( list, lock, event ):
for i in range( 10 ):
lock.acquire()
try:
list.append( i )
finally:
lock.release()
event.set()
time.sleep( 3 )
def read_from_list( list, lock, event ):
while True:
event.wait()
print( "Waking up." )
event.clear()
if len( list ):
print( "List contains " + str( list[ 0 ]) + "." )
lock.acquire()
try:
del list[ 0 ]
finally:
lock.release()
else:
print( "List is empty." )
list = []
lock = threading.Lock()
event = threading.Event()
threading.Thread( target=write_to_list, args=[ list, lock, event ]).start() threading.Thread( target=read_from_list, args=[ list, lock, event ]).start()
In basketball, first you must learn to dribble and pass,
before you can begin to shoot.
With certain reservations, texts that can be considered
to learn Python are:
"Object-Oriented Programming in Python Documentation" - a PDF file, Introduction to Programming Using Python - Y Daniel Liang (2013),
How to Think Like a Computer Scientist - Peter Wentworth (2012-08-12),
The Coder's Apprentice - Pieter Spronck (2016-09-21), and
Python Programming - John Zelle (2009).
I'm in principle ok with locks, if it must be.
Concurrent programming is quite difficult, plus you better think
in terms of queues than shared data...
better option for concurrent data access is use a (relational)
database, then the appropriate transaction isolation levels
when reading and/or writing.
Julio
tI would like to write a program, that reads from the network a fixed
amount of bytes and appends them to a list. This should happen once a
second.
Another part of the program should take the list, as it has been
filled so far, every 6 hours or so, and do some computations on the
data (a FFT).
Every so often (say once a week) the list should be saved to a file, shorthened in the front by so many items, and filled further with the
data coming fom the network. After the first saving of the whole list,
only the new part (the data that have come since the last saving)
should be appended to the file. A timestamp is in the data, so it's
easy to say what is new and what was already there.
I'm not sure how to do this properly: can I write a part of a program
that keeps doing its job (appending data to the list once every
second) while another part computes something on the data of the same
list, ignoring the new data being written?
Basically the question boils down to wether it is possible to have
parts of a program (could be functions) that keep doing their job
while other parts do something else on the same data, and what is the
best way to do this.
tI would like to write a program, that reads from the network a fixed
amount of bytes and appends them to a list. This should happen once a
second.
Another part of the program should take the list, as it has been filled
so far, every 6 hours or so, and do some computations on the data (a FFT).
Every so often (say once a week) the list should be saved to a file,
shorthened in the front by so many items, and filled further with the
data coming fom the network. After the first saving of the whole list,
only the new part (the data that have come since the last saving) should
be appended to the file. A timestamp is in the data, so it's easy to say
what is new and what was already there.
I'm not sure how to do this properly: can I write a part of a program
that keeps doing its job (appending data to the list once every second)
while another part computes something on the data of the same list,
ignoring the new data being written?
Thank you for your reply.<snip>
On 08.08.22 14:55, Julio Di Egidio wrote:
Concurrent programming is quite difficult, plus you better think
in terms of queues than shared data...
Do you mean queues in the sense of deque (the data structure)?
But, an easier and often
better option for concurrent data access is use a (relational)
database, then the appropriate transaction isolation levels
when reading and/or writing.
That would obviusly save some coding (but would introduce the need to
code the interaction with the database), but I'm not sure it would speed
up the thing. Would the RDBMS allow to read a table while something else
is writing to it? I doubt it and I'm not sure it doesn't flush the cache before letting you read, which would include a normally slow disk access.
On Monday, 8 August 2022 at 19:39:48 UTC+2, Andreas Croci wrote:<snip>
Here is just the first link I have found, but there is quite some
literature on the subject of concurrent programming and primitives: <https://duckduckgo.com/?q=async+message+queue>
Yes, but this is difficult.
Andreas Croci <andrea.croci@gmx.de> writes:
Basically the question boils down to wether it is possible to have parts
of a program (could be functions) that keep doing their job while other >>parts do something else on the same data, and what is the best way to do >>this.
Yes, but this is difficult. If you ask this question here,
you might not be ready for this.
I haven't learned it yet myself, but nevertheless tried to
write a small example program quickly, which might still
contain errors because of my lack of education.
import threading
import time
def write_to_list( list, lock, event ):
for i in range( 10 ):
lock.acquire()
try:
list.append( i )
finally:
lock.release()
event.set()
time.sleep( 3 )
def read_from_list( list, lock, event ):
while True:
event.wait()
print( "Waking up." )
event.clear()
if len( list ):
print( "List contains " + str( list[ 0 ]) + "." )
lock.acquire()
try:
del list[ 0 ]
finally:
lock.release()
else:
print( "List is empty." )
list = []
lock = threading.Lock()
event = threading.Event()
threading.Thread( target=write_to_list, args=[ list, lock, event ]).start() threading.Thread( target=read_from_list, args=[ list, lock, event ]).start()
In basketball, first you must learn to dribble and pass,
before you can begin to shoot.
With certain reservations, texts that can be considered
to learn Python are:
"Object-Oriented Programming in Python Documentation" - a PDF file, Introduction to Programming Using Python - Y Daniel Liang (2013),
How to Think Like a Computer Scientist - Peter Wentworth (2012-08-12),
The Coder's Apprentice - Pieter Spronck (2016-09-21), and
Python Programming - John Zelle (2009).
On 8 Aug 2022, at 20:24, MRAB <python@mrabarnett.plus.com> wrote:
On 2022-08-08 12:20, Stefan Ram wrote:
Andreas Croci <andrea.croci@gmx.de> writes:When working with threads, you should use queues, not lists, because queues do their own locking and can wait for items to arrive, with a timeout, if desired:
Basically the question boils down to wether it is possible to have parts of a program (could be functions) that keep doing their job while other parts do something else on the same data, and what is the best way to do this.Yes, but this is difficult. If you ask this question here,
you might not be ready for this.
I haven't learned it yet myself, but nevertheless tried to
write a small example program quickly, which might still
contain errors because of my lack of education.
import threading
import time
def write_to_list( list, lock, event ):
for i in range( 10 ):
lock.acquire()
try:
list.append( i )
finally:
lock.release()
event.set()
time.sleep( 3 )
def read_from_list( list, lock, event ):
while True:
event.wait()
print( "Waking up." )
event.clear()
if len( list ):
print( "List contains " + str( list[ 0 ]) + "." )
lock.acquire()
try:
del list[ 0 ]
finally:
lock.release()
else:
print( "List is empty." )
list = []
lock = threading.Lock()
event = threading.Event()
threading.Thread( target=write_to_list, args=[ list, lock, event ]).start() >> threading.Thread( target=read_from_list, args=[ list, lock, event ]).start() >> In basketball, first you must learn to dribble and pass,
before you can begin to shoot.
With certain reservations, texts that can be considered
to learn Python are:
"Object-Oriented Programming in Python Documentation" - a PDF file,
Introduction to Programming Using Python - Y Daniel Liang (2013),
How to Think Like a Computer Scientist - Peter Wentworth (2012-08-12),
The Coder's Apprentice - Pieter Spronck (2016-09-21), and
Python Programming - John Zelle (2009).
import queue
import threading
import time
def write_to_item_queue(item_queue):
for i in range(10):
print("Put", i, "in queue.", flush=True)
item_queue.put(i)
time.sleep(3)
# Using None to indicate that there's no more to come.
item_queue.put(None)
def read_from_item_queue(item_queue):
while True:
try:
item = item_queue.get()
except item_queue.Empty:
print("Queue is empty; should've have got here!", flush=True)
else:
print("Queue contains " + str(item) + ".", flush=True)
if item is None:
# Using None to indicate that there's no more to come.
break
item_queue = queue.Queue()
write_thread = threading.Thread(target=write_to_item_queue, args=[item_queue])
write_thread.start()
read_thread = threading.Thread(target=read_from_item_queue, args=[item_queue])
read_thread.start()
# Wait for the threads to finish.
write_thread.join()
read_thread.join()
print("Finished.")
--
https://mail.python.org/mailman/listinfo/python-list
I'm in principle ok with locks, if it must be. What I fear is that the lock could last long and prevent the function that writes into the list from
doing so every second. With an FFT on a list that contains a few bytes taken every second over one week time (604.800 samples), I believe it's very
likely that the FFT function takes longer than a second to return.
Andreas Croci <andrea.croci@gmx.de> writes:
Basically the question boils down to wether it is possible to have parts
of a program (could be functions) that keep doing their job while other >>parts do something else on the same data, and what is the best way to do >>this.
Yes, but this is difficult. If you ask this question here,
you might not be ready for this.
lock.acquire()
try:
list.append( i )
finally:
lock.release()
Basically the question boils down to wether it is possible to have
parts of a program (could be functions) that keep doing their job while
other parts do something else on the same data, and what is the best
way to do this.
tI would like to write a program, that reads from the network a fixed
amount of bytes and appends them to a list. This should happen once a
second.
Another part of the program should take the list, as it has been filled
so far, every 6 hours or so, and do some computations on the data (a FFT).
Every so often (say once a week) the list should be saved to a file, shorthened in the front by so many items, and filled further with the
data coming fom the network. After the first saving of the whole list,
only the new part (the data that have come since the last saving) should
be appended to the file. A timestamp is in the data, so it's easy to say
what is new and what was already there.
I'm not sure how to do this properly: can I write a part of a program
that keeps doing its job (appending data to the list once every second)
while another part computes something on the data of the same list,
ignoring the new data being written?
Basically the question boils down to wether it is possible to have parts
of a program (could be functions) that keep doing their job while other
parts do something else on the same data, and what is the best way to do this.
Do you mean queues in the sense of deque (the data structure)? I ask
because I can see the advantage there when I try to pop data from the
front of it, but I don't see the sense of the following statement ("than
That would obviusly save some coding (but would introduce the need to
code the interaction with the database), but I'm not sure it would speed
up the thing. Would the RDBMS allow to read a table while something else
is writing to it? I doubt it and I'm not sure it doesn't flush the cache >before letting you read, which would include a normally slow disk access.
On Mon, 8 Aug 2022 at 19:01, Andreas Croci <andrea.croci@gmx.de> wrote:
Basically the question boils down to wether it is possible to have
parts
of a program (could be functions) that keep doing their job while other
parts do something else on the same data, and what is the best way to do
this.
Why do these "parts of a program" need to be part of the *same*
program. I would write this as just two separate programs. One
collects the data and writes it to a file. The other periodically
reads the file and computes the DFT.
I would like to write a program, that reads from the network a fixed
amount of bytes and appends them to a list. This should happen once a
second.
Why would this application *require* parallel programming? This could be done in one, single thread program. Call time to get time and save it as start_time. Keep a count of the number of 6 hour intervals, initialize it to 0.
Why would this application *require* parallel programming? This could
be done in one, single thread program. Call time to get time and save
it as start_time. Keep a count of the number of 6 hour intervals,
initialize it to 0.
Schachner, Joseph (US) wrote at 2022-8-9 17:04 +0000:
Why would this application *require* parallel programming? This could be done in one, single thread program. Call time to get time and save it as start_time. Keep a count of the number of 6 hour intervals, initialize it to 0.
You could also use the `sched` module from Python's library.
Why would this application *require* parallel programming? This could be done in one, single thread program. Call time to get time and save it as start_time. Keep a count of the number of 6 hour intervals, initialize itto 0.
On Wed, 10 Aug 2022 19:33:04 +0200, "Dieter Maurer" <dieter@handshake.de> declaimed the following:[...]
Schachner, Joseph (US) wrote at 2022-8-9 17:04 +0000:
Why would this application *require* parallel programming? This
could be done in one, single thread program. Call time to get time
and save it as start_time. Keep a count of the number of 6 hour >>intervals, initialize it to 0.
Though if I read this correctly, a long running action /will/
delay others -- which could mean the (FFT) process could block
collecting new 1-second readings while it is active.
On 2022-08-09 at 17:04:51 +0000,
"Schachner, Joseph (US)" <Joseph.Schachner@Teledyne.com> wrote:
Why would this application *require* parallel programming? This could
be done in one, single thread program. Call time to get time and save
it as start_time. Keep a count of the number of 6 hour intervals, initialize it to 0.
In theory, you are correct.
In practice, [stuff] happens. What if your program crashes? Or the
computer crashes? Or there's a Python update? Or an OS update? Where
does all that pending data go, and how will you recover it after you've addressed whatever happened? ¹
OTOH, once you start writing the pending data to a file, then it's an extremely simple leap to multiple programs (rather than multiple
threads) for all kinds of good reasons.
¹ FWIW, I used to develop highly available systems, such as telephone switches, which allow [stuff] to happen, and yet continue to function.
It's pretty cool to yank a board (yes, physically remove it, without
warning) from the system without [apparently] disrupting anything. Such systems also allow for hardware, OS, and application upgrades, too
(IIRC, we were allowed a handful of seconds of downtime per year to meet
our availability requirements). That said, designing and building such
a system for the sakes of simplicity and convenience of the application
we're talking about here would make a pretty good definition of
"overkill."
--
https://mail.python.org/mailman/listinfo/python-list
On 2022-08-10 14:19:37 -0400, Dennis Lee Bieber wrote:
On Wed, 10 Aug 2022 19:33:04 +0200, "Dieter Maurer" <dieter@handshake.de
declaimed the following:[...]
Schachner, Joseph (US) wrote at 2022-8-9 17:04 +0000:
Why would this application *require* parallel programming? This
could be done in one, single thread program. Call time to get time >>and save it as start_time. Keep a count of the number of 6 hour >>intervals, initialize it to 0.
Though if I read this correctly, a long running action /will/
delay others -- which could mean the (FFT) process could block
collecting new 1-second readings while it is active.
Certainly, but does it matter? Data is received from some network
connection and network connections often involve quite a bit of
buffering. If the consumer is blocked for 3 or 4 or maybe even 20
seconds, the producer might not even notice. (This of course depends
very much on the details which we know nothing about.)
hp
--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
--
https://mail.python.org/mailman/listinfo/python-list
On Wed, 10 Aug 2022 19:33:04 +0200, "Dieter Maurer" <dieter@handshake.de>
...
You could also use the `sched` module from Python's library.
<sigh> Time to really read the library reference manual again...
Though if I read this correctly, a long running action /will/ delay
others -- which could mean the (FFT) process could block collecting new >1-second readings while it is active. It also is "one-shot" on the
scheduled actions, meaning those actions still have to reschedule
themselves for the next time period.
tI would like to write a program, that reads from the network a fixed
amount of bytes and appends them to a list. This should happen once a
second.
Another part of the program should take the list, as it has been filled
so far, every 6 hours or so, and do some computations on the data (a FFT).
Every so often (say once a week) the list should be saved to a file, shorthened in the front by so many items, and filled further with the
data coming fom the network. After the first saving of the whole list,
only the new part (the data that have come since the last saving) should
be appended to the file. A timestamp is in the data, so it's easy to say
what is new and what was already there.
I'm not sure how to do this properly: can I write a part of a program
that keeps doing its job (appending data to the list once every second)
while another part computes something on the data of the same list,
ignoring the new data being written?
Basically the question boils down to wether it is possible to have parts
of a program (could be functions) that keep doing their job while other
parts do something else on the same data, and what is the best way to do this.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 295 |
Nodes: | 16 (2 / 14) |
Uptime: | 18:00:13 |
Calls: | 6,640 |
Files: | 12,187 |
Messages: | 5,325,127 |