Skip to content

Commit 4edc1e6

Browse files
committed
async description
1 parent 666a1cd commit 4edc1e6

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

index.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,35 @@ sourceFiles.forEach(file => {
2929
const mdPath = path.join(__dirname, 'md', newName + '.md');
3030

3131
// 1. pdf to xml
32-
console.log(`Transforming ${newName}.pdf to ${newName}.xml...`);
32+
console.log(`Transforming pdf to xml (${newName}) ...`);
3333
child_process.execSync(`python ${pythonTool} -o ${xmlPath} ${pdfPath}`);
3434

3535
// 2. xml to json
36-
console.log(`Transforming ${newName}.xml to ${newName}.json...`);
36+
console.log(`Transforming xml to json (${newName}) ...`);
3737
child_process.execSync(`node ./scripts/xml2json.js ${xmlPath} ${jsonPath}`);
3838

3939
// 3. json to txt
40-
console.log(`Transforming ${newName}.json to ${newName}.txt...`);
40+
console.log(`Transforming json to txt (${newName}) ...`);
4141
child_process.execSync(`node ./scripts/json2txt.js ${jsonPath} ${txtPath}`);
4242

43-
// 4. 提取摘要和结论
44-
console.log(`Extracting structural information from ${newName}.txt to ${newName}.md...`);
43+
// 4. extract useful information
44+
console.log(`Extracting structural information from txt to md (${newName}) ...`);
4545
const info = child_process.execSync(`node ./scripts/txt2md.js ${txtPath} ${mdPath}`, { encoding: 'utf8' });
4646
console.log(info);
4747

4848
// 5. translation,两种写法均可
49-
// 要实时显示程序输出,得用异步版的.spawn()
50-
// 但异步版的.spawn()堵塞不住主进程,如果有多个文件需要翻译,循环中,主进程继续运行到.spawn()就会再开一个不堵塞的异步子进程。这样就可以出现多个子进程同时访问服务器,虽然翻译地快,但容易被封杀,特别是文件多的时候。
49+
50+
/**
51+
* 要实时显示程序输出,得用异步版的.spawn(),但异步版的.spawn()堵塞不住主进程。
52+
* 如果有多个文件需要处理,程序便会继续进行下一次迭代,运行到下一个.spawn()时,就会再开一个异步子进程。如果前面文件的翻译工作尚未完成,便会有多个子进程同时访问服务器。
53+
* 好处是并行翻译多个文件,翻译得快;坏处是每个子进程的访问频率虽然被限定了,但多个子进程同时访问事实上突破了设定的频率,容易被封杀,特别是文件多的时候。
54+
*/
55+
56+
// (1). 堵塞主进程的同步版,同一时间段只有一个子进程,只翻译一个文件
5157
// const output = child_process.execSync(`node ./scripts/translate.js ${mdPath}`, { encoding: 'utf8' });
5258
// console.log(output);
5359

60+
// (2). 不堵塞主进程的异步版,同一时间段可能存在多个子进程,在翻译多个文件
5461
const stream = child_process.spawn('node', ['./scripts/translate.js', mdPath], { shell: true });
5562
stream.stdout.on('data', data => {
5663
console.log(`Translating: ${data}`);

0 commit comments

Comments
 (0)