Pyosmium File Reader не работает в многопроцессорной
Я сделал для чтения файлов OSM с PyOsmium, и оказывается, что если вы запускаете анализатор файлов в основном процессе (osm.SimpleHandler.apply_file
), а затем запустить еще один в потоке, он замерзнет, и процесс останется в памяти.
Вот код, и вы можете раскомментировать чтение первой строки nc.apply_file(input_file)
и посмотреть результат.
Попробуйте комментировать и раскомментировать эти вызовы apply_file, и посмотрите результат. Видимо, если он вызывается в основном процессе, любой другой apply_file
замерзнет
Я сделал обходной путь, поместил apply_file в тот же процесс. Но в чем причина проблемы и есть ли исправления?
import sys
from multiprocessing import Queue, Process
import osmium as osm
input_file = sys.argv[1] # give it an .osm.pbf file
readq = Queue()
class WayCounter(osm.SimpleHandler):
ways_count = 0
def way(self, w):
self.ways_count += 1
nc = WayCounter()
# ========= running this line blocks the reader process =================
#nc.apply_file(input_file)
def reader():
nc.apply_file(input_file) # works if here
print(f'counted ways: {nc.ways_count}')
class FootPathFinder(osm.SimpleHandler):
def way(self, w):
if 'highway' not in w.tags and w.tags.get('railway') != 'platform':
return
print('working')
tags = {t.k: t.v for t in list(w.tags)}
try:
coords = [(n.lon, n.lat) for n in w.nodes]
except osm.InvalidLocationError:
return
refs = [i.ref for i in w.nodes]
readq.put((tags, coords, refs))
pf = FootPathFinder()
pf.apply_file(input_file, locations=True)
readq.put(None)
reader_proc = Process(target=reader)
reader_proc.start()
while True:
data = readq.get()
if data is None:
break
reader_proc.join()