Fix concurrent script loading with force_redownload
State: closed

I added `lock_importable_file` in `get_dataset_builder_class` and `extend_dataset_builder_for_streaming` to fix the issue, and I also added a test

cc @clefourrier 

Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/datasets/pr_6718). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

<details>
<summary>Show benchmarks</summary>

PyArrow==8.0.0

<details>
<summary>Show updated benchmarks!</summary>

### Benchmark: benchmark_array_xd.json

| metric | read_batch_formatted_as_numpy after write_array2d | read_batch_formatted_as_numpy after write_flattened_sequence | read_batch_formatted_as_numpy after write_nested_sequence | read_batch_unformated after write_array2d | read_batch_unformated after write_flattened_sequence | read_batch_unformated after write_nested_sequence | read_col_formatted_as_numpy after write_array2d | read_col_formatted_as_numpy after write_flattened_sequence | read_col_formatted_as_numpy after write_nested_sequence | read_col_unformated after write_array2d | read_col_unformated after write_flattened_sequence | read_col_unformated after write_nested_sequence | read_formatted_as_numpy after write_array2d | read_formatted_as_numpy after write_flattened_sequence | read_formatted_as_numpy after write_nested_sequence | read_unformated after write_array2d | read_unformated after write_flattened_sequence | read_unformated after write_nested_sequence | write_array2d | write_flattened_sequence | write_nested_sequence |
|--------|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 0.005074 / 0.011353 (-0.006279) | 0.003505 / 0.011008 (-0.007503) | 0.063683 / 0.038508 (0.025175) | 0.029308 / 0.023109 (0.006199) | 0.246648 / 0.275898 (-0.029250) | 0.265546 / 0.323480 (-0.057933) | 0.004108 / 0.007986 (-0.003878) | 0.002683 / 0.004328 (-0.001646) | 0.048634 / 0.004250 (0.044383) | 0.043786 / 0.037052 (0.006733) | 0.262197 / 0.258489 (0.003708) | 0.291582 / 0.293841 (-0.002259) | 0.027472 / 0.128546 (-0.101074) | 0.010213 / 0.075646 (-0.065434) | 0.206744 / 0.419271 (-0.212527) | 0.036195 / 0.043533 (-0.007337) | 0.249090 / 0.255139 (-0.006049) | 0.280002 / 0.283200 (-0.003198) | 0.018568 / 0.141683 (-0.123115) | 1.124844 / 1.452155 (-0.327311) | 1.159358 / 1.492716 (-0.333359) |

### Benchmark: benchmark_getitem\_100B.json

| metric | get_batch_of\_1024\_random_rows | get_batch_of\_1024\_rows | get_first_row | get_last_row |
|--------|---|---|---|---|
| new / old (diff) | 0.093186 / 0.018006 (0.075180) | 0.302331 / 0.000490 (0.301842) | 0.000217 / 0.000200 (0.000017) | 0.000046 / 0.000054 (-0.000008) |

### Benchmark: benchmark_indices_mapping.json

| metric | select | shard | shuffle | sort | train_test_split |
|--------|---|---|---|---|---|
| new / old (diff) | 0.018727 / 0.037411 (-0.018684) | 0.061730 / 0.014526 (0.047204) | 0.074330 / 0.176557 (-0.102226) | 0.119769 / 0.737135 (-0.617366) | 0.075611 / 0.296338 (-0.220727) |

### Benchmark: benchmark_iterating.json

| metric | read 5000 | read 50000 | read_batch 50000 10 | read_batch 50000 100 | read_batch 50000 1000 | read_formatted numpy 5000 | read_formatted pandas 5000 | read_formatted tensorflow 5000 | read_formatted torch 5000 | read_formatted_batch numpy 5000 10 | read_formatted_batch numpy 5000 1000 | shuffled read 5000 | shuffled read 50000 | shuffled read_batch 50000 10 | shuffled read_batch 50000 100 | shuffled read_batch 50000 1000 | shuffled read_formatted numpy 5000 | shuffled read_formatted_batch numpy 5000 10 | shuffled read_formatted_batch numpy 5000 1000 |
|--------|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 0.285063 / 0.215209 (0.069854) | 2.824809 / 2.077655 (0.747155) | 1.481858 / 1.504120 (-0.022262) | 1.350193 / 1.541195 (-0.191002) | 1.358012 / 1.468490 (-0.110478) | 0.557842 / 4.584777 (-4.026935) | 2.380729 / 3.745712 (-1.364983) | 2.798891 / 5.269862 (-2.470970) | 1.719288 / 4.565676 (-2.846388) | 0.061705 / 0.424275 (-0.362570) | 0.005431 / 0.007607 (-0.002176) | 0.343233 / 0.226044 (0.117189) | 3.375223 / 2.268929 (1.106295) | 1.838188 / 55.444624 (-53.606436) | 1.570015 / 6.876477 (-5.306461) | 1.573157 / 2.142072 (-0.568915) | 0.650678 / 4.805227 (-4.154549) | 0.116412 / 6.500664 (-6.384252) | 0.041754 / 0.075469 (-0.033715) |

### Benchmark: benchmark_map_filter.json

| metric | filter | map fast-tokenizer batched | map identity | map identity batched | map no-op batched | map no-op batched numpy | map no-op batched pandas | map no-op batched pytorch | map no-op batched tensorflow |
|--------|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 0.970431 / 1.841788 (-0.871357) | 11.317128 / 8.074308 (3.242819) | 9.691240 / 10.191392 (-0.500152) | 0.142260 / 0.680424 (-0.538164) | 0.014131 / 0.534201 (-0.520070) | 0.289910 / 0.579283 (-0.289373) | 0.265648 / 0.434364 (-0.168715) | 0.323130 / 0.540337 (-0.217208) | 0.447005 / 1.386936 (-0.939931) |

</details>
PyArrow==latest

<details>
<summary>Show updated benchmarks!</summary>

### Benchmark: benchmark_array_xd.json

| metric | read_batch_formatted_as_numpy after write_array2d | read_batch_formatted_as_numpy after write_flattened_sequence | read_batch_formatted_as_numpy after write_nested_sequence | read_batch_unformated after write_array2d | read_batch_unformated after write_flattened_sequence | read_batch_unformated after write_nested_sequence | read_col_formatted_as_numpy after write_array2d | read_col_formatted_as_numpy after write_flattened_sequence | read_col_formatted_as_numpy after write_nested_sequence | read_col_unformated after write_array2d | read_col_unformated after write_flattened_sequence | read_col_unformated after write_nested_sequence | read_formatted_as_numpy after write_array2d | read_formatted_as_numpy after write_flattened_sequence | read_formatted_as_numpy after write_nested_sequence | read_unformated after write_array2d | read_unformated after write_flattened_sequence | read_unformated after write_nested_sequence | write_array2d | write_flattened_sequence | write_nested_sequence |
|--------|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 0.005322 / 0.011353 (-0.006031) | 0.003755 / 0.011008 (-0.007253) | 0.049646 / 0.038508 (0.011138) | 0.029669 / 0.023109 (0.006560) | 0.284151 / 0.275898 (0.008253) | 0.298351 / 0.323480 (-0.025128) | 0.004183 / 0.007986 (-0.003803) | 0.002683 / 0.004328 (-0.001645) | 0.048814 / 0.004250 (0.044563) | 0.045017 / 0.037052 (0.007965) | 0.287358 / 0.258489 (0.028869) | 0.317394 / 0.293841 (0.023553) | 0.030025 / 0.128546 (-0.098521) | 0.010854 / 0.075646 (-0.064793) | 0.058694 / 0.419271 (-0.360578) | 0.052287 / 0.043533 (0.008754) | 0.279038 / 0.255139 (0.023899) | 0.295442 / 0.283200 (0.012242) | 0.019413 / 0.141683 (-0.122270) | 1.146106 / 1.452155 (-0.306048) | 1.197777 / 1.492716 (-0.294939) |

### Benchmark: benchmark_getitem\_100B.json

| metric | get_batch_of\_1024\_random_rows | get_batch_of\_1024\_rows | get_first_row | get_last_row |
|--------|---|---|---|---|
| new / old (diff) | 0.092191 / 0.018006 (0.074184) | 0.302672 / 0.000490 (0.302182) | 0.000623 / 0.000200 (0.000423) | 0.000048 / 0.000054 (-0.000006) |

### Benchmark: benchmark_indices_mapping.json

| metric | select | shard | shuffle | sort | train_test_split |
|--------|---|---|---|---|---|
| new / old (diff) | 0.022067 / 0.037411 (-0.015345) | 0.081760 / 0.014526 (0.067235) | 0.087548 / 0.176557 (-0.089009) | 0.126405 / 0.737135 (-0.610730) | 0.089331 / 0.296338 (-0.207008) |

### Benchmark: benchmark_iterating.json

| metric | read 5000 | read 50000 | read_batch 50000 10 | read_batch 50000 100 | read_batch 50000 1000 | read_formatted numpy 5000 | read_formatted pandas 5000 | read_formatted tensorflow 5000 | read_formatted torch 5000 | read_formatted_batch numpy 5000 10 | read_formatted_batch numpy 5000 1000 | shuffled read 5000 | shuffled read 50000 | shuffled read_batch 50000 10 | shuffled read_batch 50000 100 | shuffled read_batch 50000 1000 | shuffled read_formatted numpy 5000 | shuffled read_formatted_batch numpy 5000 10 | shuffled read_formatted_batch numpy 5000 1000 |
|--------|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 0.295821 / 0.215209 (0.080612) | 2.897930 / 2.077655 (0.820276) | 1.604500 / 1.504120 (0.100380) | 1.471502 / 1.541195 (-0.069692) | 1.497918 / 1.468490 (0.029428) | 0.576179 / 4.584777 (-4.008598) | 2.452103 / 3.745712 (-1.293609) | 2.668043 / 5.269862 (-2.601818) | 1.753544 / 4.565676 (-2.812133) | 0.064410 / 0.424275 (-0.359865) | 0.005027 / 0.007607 (-0.002580) | 0.351509 / 0.226044 (0.125465) | 3.479208 / 2.268929 (1.210280) | 1.990356 / 55.444624 (-53.454269) | 1.684920 / 6.876477 (-5.191556) | 1.794251 / 2.142072 (-0.347821) | 0.662692 / 4.805227 (-4.142535) | 0.118589 / 6.500664 (-6.382076) | 0.040813 / 0.075469 (-0.034656) |

### Benchmark: benchmark_map_filter.json

| metric | filter | map fast-tokenizer batched | map identity | map identity batched | map no-op batched | map no-op batched numpy | map no-op batched pandas | map no-op batched pytorch | map no-op batched tensorflow |
|--------|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 1.002390 / 1.841788 (-0.839398) | 12.004617 / 8.074308 (3.930309) | 10.216005 / 10.191392 (0.024613) | 0.154354 / 0.680424 (-0.526070) | 0.015554 / 0.534201 (-0.518647) | 0.288741 / 0.579283 (-0.290542) | 0.276774 / 0.434364 (-0.157590) | 0.327055 / 0.540337 (-0.213282) | 0.435121 / 1.386936 (-0.951815) |

</details>
</details>

![](https://cml.dev/watermark.png#f45bc6caa25115a04c41b278671a5a89457eb66c "CML watermark")