更新导出csv进度显示
Showing
2 changed files
with
33 additions
and
2 deletions
records.csv
0 → 100644
This diff could not be displayed because it is too large.
| ... | @@ -103,8 +103,37 @@ def _encode_url(url: str) -> str: | ... | @@ -103,8 +103,37 @@ def _encode_url(url: str) -> str: |
| 103 | return urllib.parse.urlunsplit(parsed._replace(path=encoded_path)) | 103 | return urllib.parse.urlunsplit(parsed._replace(path=encoded_path)) |
| 104 | 104 | ||
| 105 | 105 | ||
| 106 | def _query_with_spinner(fn, *args, **kwargs): | ||
| 107 | """在后台线程执行 fn,主线程每秒打印一次进度点,完成后换行。""" | ||
| 108 | import sys | ||
| 109 | result_box: list = [] | ||
| 110 | exc_box: list = [] | ||
| 111 | |||
| 112 | def _run(): | ||
| 113 | try: | ||
| 114 | result_box.append(fn(*args, **kwargs)) | ||
| 115 | except Exception as e: | ||
| 116 | exc_box.append(e) | ||
| 117 | |||
| 118 | t = threading.Thread(target=_run, daemon=True) | ||
| 119 | t.start() | ||
| 120 | spinner = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"] | ||
| 121 | i = 0 | ||
| 122 | while t.is_alive(): | ||
| 123 | sys.stderr.write(f"\r{spinner[i % len(spinner)]} 查询中...") | ||
| 124 | sys.stderr.flush() | ||
| 125 | t.join(timeout=0.1) | ||
| 126 | i += 1 | ||
| 127 | sys.stderr.write("\r查询完成。 \n") | ||
| 128 | sys.stderr.flush() | ||
| 129 | |||
| 130 | if exc_box: | ||
| 131 | raise exc_box[0] | ||
| 132 | return result_box[0] | ||
| 133 | |||
| 134 | |||
| 106 | def fetch_records( | 135 | def fetch_records( |
| 107 | conn: psycopg.Connection, | 136 | conn, |
| 108 | song_ids: list[int] | None, | 137 | song_ids: list[int] | None, |
| 109 | extra_versions: int, | 138 | extra_versions: int, |
| 110 | song_limit: int, | 139 | song_limit: int, |
| ... | @@ -279,7 +308,9 @@ def main() -> None: | ... | @@ -279,7 +308,9 @@ def main() -> None: |
| 279 | logger.error("缺少 psycopg,请执行: pip install psycopg") | 308 | logger.error("缺少 psycopg,请执行: pip install psycopg") |
| 280 | raise SystemExit(1) | 309 | raise SystemExit(1) |
| 281 | with psycopg.connect(args.dsn) as conn: | 310 | with psycopg.connect(args.dsn) as conn: |
| 282 | records = fetch_records( | 311 | logger.info("查询记录中(数据量较大,请稍候)...") |
| 312 | records = _query_with_spinner( | ||
| 313 | fetch_records, | ||
| 283 | conn, | 314 | conn, |
| 284 | song_ids=song_ids, | 315 | song_ids=song_ids, |
| 285 | extra_versions=args.extra_versions, | 316 | extra_versions=args.extra_versions, | ... | ... |
-
Please register or sign in to post a comment