Pythonicity of some algorithms

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David

    Pythonicity of some algorithms

    Hi list.

    I have 2 questions about the Pythonicity of some algorithms. One
    question is about generator usage, the other is about producer and
    consumer threads

    GENERATOR QUESTION

    Usually for generators, the usage is like this:

    for var in generator():
    do_something(va r)

    I sometimes have cases where I want to use a generator, but I don't
    want to stall the main program if the generator function doesn't have
    any new data for me (maybe it's retrieving data from a device, but I
    want to do other things with the same device if there isn't any data
    yet).

    So currently, some of my code looks something like this:

    def get_serial_data ():
    while True:
    block = request_a_packe t_from_serial_d evice() # Get some raw data
    parsed_list = parse_block(blo ck) # Extract a list of records
    from the packet
    if parsed_list:
    # The record contains 1 or more records
    for record in parse_list:
    yield record
    else:
    # Sometimes there aren't any records in the packet
    yield None

    def thread_function _for_handling_s erial_device():
    record_iter = iter(get_serial _data())
    while True:
    record = record_iter.nex t()
    if record:
    handle_record() # eg: add to a queue for the main thread to process
    other_serial_lo gic1()
    other_serial_lo gic2()
    other_serial_lo gic3()

    This works for me. But I'd like to know if this is considered
    Pythonic, and if there are other, better ways of doing the above in
    Python.

    PRODUCER AND CONSUMER THREAD QUESTION

    In some of my Python apps I use the producer/consumer model, where the
    producer and consumer threads communicate through Queues.

    Usually code (in the consumer thread function) looks something like this:

    def consumer_thread ():
    while True:
    record = some_queue.get( )
    process_record( record)

    In some cases I'd like to do something unusual to threads consuming a
    queue - interrupt them, make them terminate, etc. The problem is that
    a lot of the time it's locking, waiting for a record from
    some_queue.get( )

    A few things I've considered:

    1) Add a timeout to some_queue.get( ), so the loop has a chance to do
    other things while waiting for new records. This is problematic
    because most of the time there isn't other things for the thread to
    do. Also, it won't be able to respond immediately to those events.

    2) Add special values to the queue, the purpose of which is to notify
    the consumers. This is problematic when you have multiple consumers.
    You have to add the exact correct number of 'interrupt' objects to the
    queue.

    3) Make a custom thread-safe queue class, with an 'interrupt_get'
    method. When your app calls queue.interrupt _get(), all threads
    currently locking on a queue.get() will continue, but with a
    GetInterrupted exception thrown.

    Perhaps there is some other Python threading idiom I should be using
    in this case. Any suggestions?

    David.
  • =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

    #2
    Re: Pythonicity of some algorithms

    This works for me. But I'd like to know if this is considered
    Pythonic, and if there are other, better ways of doing the above in
    Python.
    From the Python point of view, it's fine. However, it uses busy-wait,
    which I consider bad style (in any language).
    3) Make a custom thread-safe queue class, with an 'interrupt_get'
    method. When your app calls queue.interrupt _get(), all threads
    currently locking on a queue.get() will continue, but with a
    GetInterrupted exception thrown.
    That's what I would do. I'd use the existing Queue class as a base
    class, though, rather than starting from scratch.

    Regards,
    Martin

    Comment

    • castironpi@gmail.com

      #3
      Re: Pythonicity of some algorithms

      On May 10, 5:21 am, "Martin v. Löwis" <mar...@v.loewi s.dewrote:
      This works for me. But I'd like to know if this is considered
      Pythonic, and if there are other, better ways of doing the above in
      Python.
      >
      From the Python point of view, it's fine. However, it uses busy-wait,
      which I consider bad style (in any language).
      >
      3) Make a custom thread-safe queue class, with an 'interrupt_get'
      method. When your app calls queue.interrupt _get(), all threads
      currently locking on a queue.get() will continue, but with a
      GetInterrupted exception thrown.
      >
      That's what I would do. I'd use the existing Queue class as a base
      class, though, rather than starting from scratch.
      >
      Regards,
      Martin
      Did you follow the links to Dev-C++? It rules house. I have
      drizzle.c compiling in Windows GDI and SDL. I'm poised to double-time
      Dev-C++ and Windows GDI, if double-tasking is on the market. Credit
      up... what's standard?

      Comment

      Working...