How to avoid max execution timeout when importing a lot of nodes programmatically?
Yes, there is a way! It's built into Drupal core, and it's there for this exact reason and purpose. You need to use the Batch API. Batch lets you, well, perform operations in batches across page loads. It automates the carrying of information between these page loads and helps you to show a nice progress bar to the user.

See:


Batch operations reference
The Examples module (which contains a batch_example module)
Countless online tutorials