-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
135 lines (104 loc) · 3.89 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
from __future__ import annotations
import io
from _winapi import CreateFile, GENERIC_READ, GENERIC_WRITE, \
FILE_FLAG_OVERLAPPED, NULL, CloseHandle, INVALID_HANDLE_VALUE, ReadFile, Overlapped, ERROR_IO_PENDING, WriteFile
import asyncio
from asyncio import IocpProactor
from asyncio.proactor_events import BaseProactorEventLoop
from typing import Tuple
import _overlapped
from utils.timer import timer, repeat, atimer
from utils.logger import Logger
logger = Logger(__name__)
CREATE_NEW = 1
CREATE_ALWAYS = 2
OPEN_ALWAYS = 4
TRUNCATE_EXISTING = 5
@repeat(10 ** 5)
def py_open():
with open("1.py", "wb") as fp:
pass
@timer
def win_read(fn):
file_handler = CreateFile(fn, GENERIC_READ, 0, NULL, OPEN_ALWAYS,
FILE_FLAG_OVERLAPPED, NULL)
if file_handler is INVALID_HANDLE_VALUE:
return -1
ov = _overlapped.Overlapped(NULL)
r: Tuple[Overlapped, int] = ReadFile(file_handler, 2 ** 10, True)
ov, error = r
if error == ERROR_IO_PENDING:
res = ov.GetOverlappedResult(False)
logger.info(res)
logger.info(len(ov.getbuffer()))
CloseHandle(file_handler)
return ov.getbuffer()
@timer
def win_write(fn, data):
file_handler = CreateFile(fn, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS,
FILE_FLAG_OVERLAPPED, NULL)
if file_handler is INVALID_HANDLE_VALUE:
return -1
ov = _overlapped.Overlapped(NULL)
r: Tuple[Overlapped, int] = WriteFile(file_handler, data, True)
ov, error = r
if error == ERROR_IO_PENDING:
res = ov.GetOverlappedResult(False)
logger.info(res)
logger.info(ov.getbuffer())
CloseHandle(file_handler)
return ov.getbuffer()
# def open(fn):
# file_handler = CreateFile(fn, GENERIC_READ, 0, NULL, OPEN_ALWAYS,
# FILE_FLAG_OVERLAPPED, NULL)
# if file_handler is INVALID_HANDLE_VALUE:
# return -1
# return File(file_handler)
class File(io.BufferedIOBase):
def __init__(self, handle):
self.handle = handle
async def read(self, __size: int | None = ...) -> bytes:
ov = _overlapped.Overlapped(NULL)
r: Tuple[Overlapped, int] = ReadFile(self.handle, 2 ** 10, True)
loop = asyncio.get_event_loop()
if isinstance(loop, BaseProactorEventLoop):
iocp: IocpProactor = loop._proactor
if await iocp.wait_for_handle(self.handle):
res = ov.GetOverlappedResult(False)
logger.info(res)
logger.info(len(ov.getbuffer()))
return res
async def test():
f = open("1.exe")
res = await f.read(1)
@atimer
async def test_read_file():
import aiofile
# fp = aiofile.open("C:\\Users\\lin\\Downloads\\MHXY-JD-3.0.393.exe")
fp = aiofile.open("C:\\Users\\lin\\Downloads\\python-3.11.1-amd64.exe")
# fut = aiofile.read_file(b"aiofile.pxd")
with open("tmp.exe", "wb") as wp:
while data := await fp.read(1024 * 1024 * 20):
wp.write(data)
@timer
def sync_test_read_file():
with open("C:\\Users\\lin\\Downloads\\MHXY-JD-3.0.393.exe", "rb") as fp:
with open("tmp.exe", "wb") as wp:
while data := fp.read(1024 * 1024 * 20):
wp.write(data)
# print(fp.read(1024))
def CompletedReadRoutine(dwErr, cbBytesRead,
lpOverLap):
print("error:", dwErr)
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
# py_open()
# data = win_read(r"D:\Downloads\android-studio-2021.2.1.15-windows.exe")
# win_write(r"1.exe", data)
# asyncio.run(test())
asyncio.run(test_read_file())
# sync_test_read_file()
# See PyCharm help at https://www.jetbrains.com/help/pycharm/