Hello pyRevit friends!
After noticing pretty long loading times, even for scripts where nothing should happen, I started investigating the issue.
I found out it is the importing of a custom module i have created in my lib folder.
It takes 3-6 seconds to load in my scripts, i dont know why it varies that much.
This is a very short code, so I started to test loading time of other custom modules. A pretty large one with nearly 1000 lines of code takes only 0,2-0,3 seconds to load. They are in the same library folder.
A test setup that only loads the “problem module” gives me a time of only 0,3 seconds.
from pyrevit import script from datetime import datetime start_time = datetime.now() import Log print datetime.now() - start_time
But importing this module in my other scripts gives me importing times of 3-6 seconds.
I´m clueless why the loading time can be that different.
Is there anything I can do to improve that? For example, how to avoid double imports like:
uidoc = __revit__.ActiveUIDocument doc = __revit__.ActiveUIDocument.Document app = __revit__.Application
Because I import that in my script and in the module.
This is the mentioned module, happy about any advice and thoughts on this topic, thanks in advance!
# -*- coding: utf-8 -*- import io import os import csv import time from datetime import datetime from pyrevit import script uidoc = __revit__.ActiveUIDocument doc = __revit__.ActiveUIDocument.Document app = __revit__.Application FileName = script.get_bundle_name() RevitFileName = doc.Title User = app.Username PROJECT_PATH = os.path.normpath(os.path.join(__file__, '../../../../')) LogFilePath = os.path.join(PROJECT_PATH, 'UserLog\LOG.csv') def LOG_Entry(): now = datetime.now() dateformat = "%d.%m.%Y" timeformat = "%X" Date = now.strftime(dateformat) Time = now.strftime(timeformat) LOG_Entry = Time+"\t"+Date+"\t"+User+"\t"+FileName+"\t"+RevitFileName return LOG_Entry def Read_LOG(): retry = 0 max_retries=5 while True: try: with io.open(LogFilePath,"r", encoding = "utf-8") as LOGfile: LOG = LOGfile.read().splitlines() except OSError: time.sleep(0.5) retry += 1 if retry > max_retries: raise else: return LOG def Write_LOG(): if os.path.exists(LogFilePath): LOG = Read_LOG() LOG_New_Entry = LOG_Entry() LOG.append(LOG_New_Entry) retry = 0 max_retries=5 while True: try: with io.open(LogFilePath,"w", encoding = "UTF8", newline='') as LOGfile: for L in LOG: writer = csv.writer(LOGfile,delimiter="'") writer.writerow([L]) except OSError: time.sleep(0.5) retry += 1 if retry > max_retries: raise else: return LOG_New_Entry