You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Inaccurate results on windows when mode=a is set and Semaphore is 2
Environment:
system: windows
aiofiles version: 0.8.0
Test
test1:
sem = asyncio.Semaphore(2)
When the parameter is passed in 1, the result is correct
When the parameter is passed in 2, the result is incorrect
test2
Wrong result on windows, correct result on linux
Source
importasyncioimportosimportaiofilesasyncdefwrite_chunk_to_file(sem: asyncio.Semaphore, chunk: str) ->None:
asyncwithsem:
asyncwithaiofiles.open("test.txt", mode="a") asf:
awaitf.write(f'{chunk}\n')
asyncdefmain() ->None:
# Prepare the data to write in batchesdata_chunks: list[str] = [
"Berlin", "Germany", "Madrid", "Spain", "Rome", "Italy",
"Washington D.C.", "USA", "Ottawa", "Canada", "Mexico City", "Mexico",
"Tokyo", "Japan", "Beijing", "China", "Manila", "Philippines"
]
# Truncate existing filefile_path='test.txt'ifos.path.isfile(file_path):
os.remove(file_path)
# Launch coroutines to write batches to the filesem=asyncio.Semaphore(1)
awaitasyncio.gather(*[write_chunk_to_file(sem, chunk) forchunkindata_chunks])
if__name__=="__main__":
asyncio.run(main())
Result
The result of running on windows when setting Semaphore to 2
expect data
1. Berlin
2. Germany
3. Madrid
4. Spain
5. Rome
6. Italy
7. Washington D.C.
8. USA
9. Ottawa
10. Canada
11. Mexico City
12. Mexico
13. Tokyo
14. Japan
15. Beijing
16. China
17. Manila
18. Philippines
actual data
1. Berlin
2. Germany
3. Madrid
4. Italy
5. Washington D.C.
6. Ottawa
7. Mexico City
8. Tokyo
9. Beijing
10. China
11. Philippines
The text was updated successfully, but these errors were encountered:
You're probably running into a race condition caused by the combination of Python and the operating system, I don't think I can fix this in aiofiles. I would suggest using a lock (or a semaphore with a value of 1) to mitigate the issue.
Inaccurate results on
windows
whenmode=a
is set and Semaphore is 2Environment:
system:
windows
aiofiles version:
0.8.0
Test
test1:
sem = asyncio.Semaphore(2)
When the parameter is passed in 1, the result is correct
When the parameter is passed in 2, the result is incorrect
test2
Wrong result on windows, correct result on linux
Source
Result
The result of running on windows when setting Semaphore to 2
expect data
actual data
The text was updated successfully, but these errors were encountered: