H
H
Holfamer2015-03-25 23:02:04
Python
Holfamer, 2015-03-25 23:02:04

How to properly close a stream in python?

How to properly close a stream? Is this possible in python? What does thread.exit() do? Is the thread alive after thread.exit()?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sly_tom_cat ., 2015-09-10
@Sly_tom_cat

Instead of it is necessary to close flows and processes from the outside. You can get in a lot of trouble.
In a well-designed solution, threads / processes themselves should be closed when necessary. If you need to manage this from the outside, then for this there are primitives such as semaphores / flags / events.
It is useful to put any search or preparation of pieces of a common solution into streams. In this case, you can use two template primitives: "collector" and "winner":
"Collector":

import threading
def job(id, ...):
global res
  <Подготовка блока данных>
  res[id] = <блок подготовленных данных>
  return

res = []
for i in range(N):   # запуск N потоков
    res.append(<пустой блок>)
    threading.Thread(target=job, args=(i, ...)).start()
for th in threading.enumerate():  # ожидание всех потоков 
    if th != threading.currentThread():
      th.join()
<здесь массив с блоками полностью собран >

"Winner":
import threading
def job(...):
  global val
  global event
  while <не нашли>
    <элементарный шаг поиска>
    if event.is_set():
      return   # кто-то уже нашел искомое
  val=<то что нашли>
  event.set()   # этот поток нашел искомое и радостно сообщает всем об этом
  return

val = None  # искомое значение
event = threading.Event() # флаг сигнализирующий о том что решение найдено
threads = []
for i in range(N):
    threads.append(threading.Thread(target=job, args=(i, ...)))
    threads[-1].start()
event.wait()  # тут основной поток ждет сигнала о победе от одного из запущенных потоков
# если есть шанс, что победителя может и не быть, то в wait() нужно указать таймаут
# и читать значение event.wait(), что бы узнать - сработал таймаут или появился победитель.
<тут обрабатываем найденное>
for th in threads:   # Подчищаем за собой - гарантированно собираем закрывшиеся потоки
      th.join()

Threat the last cycle in the "winner" is often not necessary, but if this procedure itself is launched in many threads, then you can get the so-called "stream flood", when the threads do not have time to close themselves and multiply in the system with terrible force, with predictable consequences.
ZYY also pay attention to the options for working with a set of threads. The "Collector" example does not imply that there are any other threads in the program, so the collection of threads is all but the current one (the main thread of the program). But in the "Winner" streams are stored in the list and then collected from this list. This allows us to have other threads in the program running in parallel with our search for a winner (and in particular, to run such a search for a winner in independent threads). True, to arrange this code in separate threads, you may need to get rid of global variables and work only through the parameters passed to the job.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question