MCPcopy
hub / github.com/psf/black / schedule_formatting

Function schedule_formatting

src/black/concurrency.py:142–221  ·  view source on GitHub ↗

Run formatting of `sources` in parallel using the provided `executor`. (Use ProcessPoolExecutors for actual parallelism.) `write_back`, `fast`, and `mode` options are passed to :func:`format_file_in_place`.

(
    sources: set[Path],
    fast: bool,
    write_back: WriteBack,
    mode: Mode,
    report: Report,
    loop: asyncio.AbstractEventLoop,
    executor: Executor,
    no_cache: bool = False,
)

Source from the content-addressed store, hash-verified

140
141
142async def schedule_formatting(
143 sources: set[Path],
144 fast: bool,
145 write_back: WriteBack,
146 mode: Mode,
147 report: Report,
148 loop: asyncio.AbstractEventLoop,
149 executor: Executor,
150 no_cache: bool = False,
151) -> None:
152 """Run formatting of `sources` in parallel using the provided `executor`.
153
154 (Use ProcessPoolExecutors for actual parallelism.)
155
156 `write_back`, `fast`, and `mode` options are passed to
157 :func:`format_file_in_place`.
158 """
159 cache = None if no_cache else Cache.read(mode)
160 if cache is not None and write_back not in (
161 WriteBack.DIFF,
162 WriteBack.COLOR_DIFF,
163 ):
164 sources, cached = cache.filtered_cached(sources)
165 for src in sorted(cached):
166 report.done(src, Changed.CACHED)
167 if not sources:
168 return
169
170 cancelled = []
171 sources_to_cache = []
172 lock = None
173 manager = None
174 if write_back in (WriteBack.DIFF, WriteBack.COLOR_DIFF):
175 # For diff output, we need locks to ensure we don't interleave output
176 # from different processes.
177 manager = Manager()
178 lock = manager.Lock()
179
180 try:
181 tasks = {
182 asyncio.ensure_future(
183 loop.run_in_executor(
184 executor, format_file_in_place, src, fast, mode, write_back, lock
185 )
186 ): src
187 for src in sorted(sources)
188 }
189 pending = tasks.keys()
190 try:
191 loop.add_signal_handler(signal.SIGINT, cancel, pending)
192 loop.add_signal_handler(signal.SIGTERM, cancel, pending)
193 except NotImplementedError:
194 # There are no good alternatives for these on Windows.
195 pass
196 while pending:
197 done, _ = await asyncio.wait(pending, return_when=asyncio.FIRST_COMPLETED)
198 for task in done:
199 src = tasks.pop(task)

Callers 1

reformat_manyFunction · 0.85

Calls 9

readMethod · 0.80
filtered_cachedMethod · 0.80
doneMethod · 0.80
LockMethod · 0.80
popMethod · 0.80
failedMethod · 0.80
writeMethod · 0.80
shutdownMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected