mozman / ezdxf

Python interface to DXF
https://ezdxf.mozman.at
MIT License
930 stars 191 forks source link

Concurrent processing of entities in dxf file #1169

Closed avinash-218 closed 1 month ago

avinash-218 commented 1 month ago

Hi, Below is the sample script that reproduces the error. I have a dxf file and i want to extract all the line entities present in it. I want to do this parallely. But it throws recursion error : Maximum recursion depth exceeded.

Below is the script

import ezdxf
import concurrent.futures

def extract_line_entity_data(entity):
    data = {}
    data["Entity Type"] = entity.dxftype()    # Store the type of entity
    data["Layer Name"] = entity.dxf.layer # Store the name of the layer to which the line entity belongs
    data["Start Point"] = tuple(entity.dxf.start) # Store the start point of the line as a tuple (x, y, z)
    data["End Point"] = tuple(entity.dxf.end) # Store the end point of the line as a tuple (x, y, z)
    return data

def extract_line_entities(file_path):
    doc = ezdxf.readfile(file_path)
    entities = doc.modelspace().query('LINE')

    extracted_line_data = []
    with concurrent.futures.ProcessPoolExecutor() as executor:
        results = list(executor.map(extract_line_entity_data, entities))

    extracted_line_data = [result for result in results if result is not None]

    return extracted_line_data

if __name__ == "__main__":
    file_path = 'Sample_Filer.dxf'  # Replace with your DXF file path
    line_entities = extract_line_entities(file_path)
    print(line_entities)

Below is the error

File "C:\Users\user1\anaconda3\envs\deploy\lib\site-packages\ezdxf\entities\dxfns.py", line 123, in __getattr__
    attrib_def: Optional[DXFAttr] = self.dxfattribs.get(key)
  File "C:\Users\user1\anaconda3\envs\deploy\lib\site-packages\ezdxf\entities\dxfns.py", line 297, in dxfattribs
    return self._entity.DXFATTRIBS
  File "C:\Users\user1\anaconda3\envs\deploy\lib\site-packages\ezdxf\entities\dxfns.py", line 123, in __getattr__
    attrib_def: Optional[DXFAttr] = self.dxfattribs.get(key)
  File "C:\Users\user1\anaconda3\envs\deploy\lib\site-packages\ezdxf\entities\dxfns.py", line 297, in dxfattribs
    return self._entity.DXFATTRIBS
  File "C:\Users\user1\anaconda3\envs\deploy\lib\site-packages\ezdxf\entities\dxfns.py", line 123, in __getattr__
    attrib_def: Optional[DXFAttr] = self.dxfattribs.get(key)
  File "C:\Users\user1\anaconda3\envs\deploy\lib\site-packages\ezdxf\entities\dxfns.py", line 297, in dxfattribs
    return self._entity.DXFATTRIBS
RecursionError: maximum recursion depth exceeded

Even if i do not extract any data from line entity, still the script is throwing the same error,

import ezdxf
import concurrent.futures

def extract_line_entity_data(entity):
    data = {}
    return data

def extract_line_entities(file_path):
    doc = ezdxf.readfile(file_path)
    entities = doc.modelspace().query('LINE')

    extracted_line_data = []
    with concurrent.futures.ProcessPoolExecutor() as executor:
        results = list(executor.map(extract_line_entity_data, entities))

    extracted_line_data = [result for result in results if result is not None]

    return extracted_line_data

if __name__ == "__main__":
    file_path = 'Sample.dxf'  # Replace with your DXF file path
    line_entities = extract_line_entities(file_path)
    print(line_entities)

Can anyone help??

mozman commented 1 month ago

Parallel processing has not been tested and is not recommended.

mozman commented 1 month ago

Addendum:

  1. Parallel processing is difficult. Solve your problem with one thread first and then parallelize the solution.
  2. Loading the DXF file is the expensive part
  3. Processing the loaded DXF file is cheap
  4. If you load the DXF file multiple times or serialize/deserialize the DXF document (if that works), performance will decrease