Pyrevit: Query regarding multiprocessing/threading for executing same functions with different arguments

Created on 9 Aug 2020  路  4Comments  路  Source: eirannejad/pyRevit

As per the code below, i call in the transaction to write shared parameter values thrice for around 1000 rooms. Each transaction takes about 30 seconds, hence total time- 90 seconds.

  • Please let me know, if i can reduce time for the processes.
  • Alternatively, could I use multiprocessing or threading to reduce #time by using parallel processing for similar functions with independent arguments. If yes could you give me some insight on the usage.
# _Function to write Shared Parameter values_
def set_parameter_by_name(element, parameterName, value):
    element.LookupParameter(parameterName).Set(value)
# _Function to perform Set Parameter By Name as a Transaction_
def set_parameter_by_name_transaction(sample_elements, sample_param_name, sample_param_values):
    t = Transaction(doc, 'script')
    write_param_pass = []
    write_param_fail = []
    t.Start()
    for e in sample_elements:
        try:
            write_param_pass = [set_parameter_by_name(e,sample_param_name,val) for e,val in zip(sample_elements, sample_param_values)]
        except:
            write_param_fail.append(val)
    t.Commit()  

# _Calling function, for clean set of parameter values_
reset_all_room_occupancies = set_parameter_by_name_transaction(rooms, 'Occupant', list_of_default_values)
# _Calling function, for writing available values_
write_occupant_count_to_rooms = set_parameter_by_name_transaction(rooms_with_valid_occupancy_values, 'Occupant', occupant_count_as_per_code_for_writing_to_param)
# _Calling function, for writing 'NA' for values not calculated_
write_occupant_count_NA_to_rooms_with_no_occupancy_type = set_parameter_by_name_transaction(rooms_with_NA_occupancy_values, 'Occupant', room_NA_occupancy_values)
Question

All 4 comments

Try running every thing you want to do to all the elements, all in one single transaction

t = Transaction(doc, 'script')
t.Start()
# none of these functions should have a transaction nested inside them
reset_all_room_occupancies()
write_occupant_count_to_rooms()
write_occupant_count_NA_to_rooms_with_no_occupancy_type()
t.Commit()  

On your question about threading, Revit is single-threaded for API changes so only the main thread can make changes.

I tried running on a single transaction, but is taking the same amount of time. I think getting Room occupancy, as per selected code, for 1000 rooms in under 90 secs, is in itself an achievement. Thank you.

Ya not too bad. Generally the Revit API is the bottleneck. Also try using the .get_Parameter() method instead of .LookupParameter. You might see some performance improvements

Will definitely try this out

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pmcmm picture pmcmm  路  3Comments

jkcameron picture jkcameron  路  5Comments

amastrobera picture amastrobera  路  3Comments

Tyree picture Tyree  路  3Comments

Arsany123 picture Arsany123  路  4Comments