{"id":19044062,"url":"https://github.com/xxxily/processassist","last_synced_at":"2026-04-11T16:43:58.452Z","repository":{"id":35116385,"uuid":"208726452","full_name":"xxxily/processAssist","owner":"xxxily","description":"child_process、fork、processAssis","archived":false,"fork":false,"pushed_at":"2022-02-12T13:56:43.000Z","size":62,"stargazers_count":1,"open_issues_count":7,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-03T01:11:07.169Z","etag":null,"topics":["childprocess","fork","nodejs","process","processassist"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xxxily.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-16T06:30:29.000Z","updated_at":"2019-09-24T09:23:10.000Z","dependencies_parsed_at":"2022-08-08T05:01:39.896Z","dependency_job_id":null,"html_url":"https://github.com/xxxily/processAssist","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xxxily%2FprocessAssist","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xxxily%2FprocessAssist/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xxxily%2FprocessAssist/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xxxily%2FprocessAssist/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xxxily","download_url":"https://codeload.github.com/xxxily/processAssist/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240100559,"owners_count":19747689,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["childprocess","fork","nodejs","process","processassist"],"created_at":"2024-11-08T22:44:44.607Z","updated_at":"2025-11-12T16:01:07.261Z","avatar_url":"https://github.com/xxxily.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# processAssist\n\n\u003e 新开子程序脚本助理，实现子父进程间函数共享与互调 \n\n## 安装\n使用 npm:\n``` bash\nnpm i process-assist  \n```\n\n使用 yarn:\n``` bash \nyarn add process-assist \n```\n\n## 使用示例\n父进程脚本（parentProcess.js）\n``` javascript\nconst ProcessAssist = require('process-assist')\nconst PA = new ProcessAssist()\n\n// 以fork的形式初始化子进程\nPA.fork('./childProcess')\n\n// 调用子进程公开的方法\nPA.child.run({\n  name: 'test',\n  params: ['test params 001']\n}).then(function (result) {\n  console.log(result)\n})\n\n// 调用子进程未公开或不存在的方法（测试）\nPA.child.run({\n  name: 'childFunc'\n}).then(function (result) {\n  console.log(result)\n})\n\n```\n\n子进程脚本（childProcess.js）\n``` javascript\nconst ProcessAssist = require('process-assist')\n\nconst shareMethod = {\n  test (info) {\n    console.log('test info:', info)\n    return 'test method result ' + info\n  }\n}\n\n// 子进程将想要共享的方法公开出去，即可在父进程中调用\nnew ProcessAssist(shareMethod)\n\n```\n\n## new ProcessAssist([publicLibs], [childProcess])\n初始化基本实例:\n```javascript\nconst ProcessAssist = require('process-assist')\nconst PA = new ProcessAssist()\n```\n* @param publicLibs {Object} -可选 要共享给父程序调用的的方法对象\n* @param childProcess {child_process} -可选 要进行通讯交流的子程序\n\n进行初始化时，publicLibs、childProcess两个参数均为可选。  \n作为父程序初始化时，一般无需进行函数共享，所以第一个参数为空，第二个参数是要控制的子程序模块，如果你已经有子程序实例，则可传入来进行初始化，如果没有，也可用通过后面的fork方法进行初始化。  \n\n如果是作为子程序，初始化时一般需要传入第一个参数，共享给父程序调用的方法对象，只有共享出去的方法，父程序才能能通过run方法调用。\n\n\n## 实例api\n### fork()\n以fork的模式运行nodejs的脚本模块，所有参数与child_process.fork参数完全一致  \n详情请参考：http://nodejs.cn/api/child_process.html#child_process_child_process_fork_modulepath_args_options  \n\n只有通过fork函数执行的脚本模块才会有IPC通道，而该脚本的实现完全基于IPC通道的消息传递机制，所以只支持fork的形式。  \n\n基本示例：\n```javascript\nPA.fork('./childProcess.js')\n```\n\n父程序初始化ProcessAssist时传入fork方法产生的子程序对象和执行fork方法产生子程序对象后都会初始化child相关的初始化逻辑，才会有下面的child下面的系列方法。   \n\n### child.run(conf)\n执行子程序共享的方法，执行完成后通知父程序执行结果\n\n* @param conf {Object} -必选 执行的配置 \n* @param conf.name {String} -必选 要操作或执行的命令/函数的名称，即子程序共享出来的方法名/函数名  \n* @param conf.params {Array} -可选 执行函数时要附带的执行参数，必须是类似arguments的数组参数  \n* @param conf.timeout {Number} -可选 子程序函数执行超时的限制，单位毫秒，默认不设置超时上限    \n* @param conf.id {String} -可选 配置id，id作为子程序执行完成后的回调关联依据，一般由程序自动生成\n* @return {Promise\u003c*\u003e} 返回一个Promise对象，子程序方法调用完成后回执行resolve回调 \n\n假如子程序共享了以下方法列表： \n```javascript\n// childProcess.js\nconst ProcessAssist = require('process-assist')\n\nconst shareMethod = {\n  test (info) {\n    console.log('test info:', info)\n    return 'test method result ' + info\n  },\n  libs:{\n    info (info) {\n      console.log('log info:', info)\n    },\n    async promiseTest () {\n      return new Promise((resolve, reject) =\u003e {\n        setTimeout(function () {\n          resolve('promise resolve')\n        }, 1000 * 3)\n      })\n    }\n  }\n}\n\nnew ProcessAssist(shareMethod)\n```\n\n父程序要调用子程序的test方法，则：\n```javascript\nPA.child.run({\n  name: 'test'\n})\n```\n\n调用子程序的test方法，并传入函数的运行参数：\n```javascript\nPA.child.run({\n  name: 'test',\n  params: ['test params 001']\n})\n```\n\n调用子程序的libs下面的log方法，并传入函数的运行参数：\n```javascript\nPA.child.run({\n  name: 'libs.info',\n  params: 'hello'\n})\n```\n此处有两个特点：\n* 通过`.`可以调用对象下的子属性\n* 传参时如果不为数组或arguments，则全部作为第一个参数处理\n\n\n调用子程序的libs下面的promiseTest方法，并传限定方法执行的超时时间：\n```javascript\nPA.child.run({\n  name: 'libs.promiseTest',\n  timeout: 1000\n}).then(function(result) {\n  console.log(result)\n})\n```\n\n因为promiseTest设置了3s后才执行resolve，而此处限定了1s后视为超时，则最终结果返回为：\n```javascript\n{\n  conf: {\n    name: 'libs.promiseTest',\n    timeout: 1000,\n    id: '15686229463521020253'\n  },\n  result: '',\n  error: '运行超时'\n}\n```\n如果不设置超时，则3s后返回如下结果：\n```javascript\n{\n  conf: { name: 'libs.promiseTest', id: '15686230316773467593' },\n  result: 'promise resolve',\n  error: false\n}\n```\n\n` 注意： 无论子程序的方法执行是出错还是被reject了，父程序都会得到resolve的结果，只是结果值中会出现error值，父程序通过判断error值来了解子程序的函数执行情况。这样设计的目的是无论子程序执行结果如何都不影响父程序的持续运行。 `\n\n\n### child.kill(signal)\n结束对子程序的占用，释放资源\n\n```javascript\nPA.child.kill()\n```\n\n### child.destroy(signal)\n跟child.kill一样\n\n### child.process\n子程序对象，即通过fork衍生出来的进程对象。\n你可以通过：\n```javascript\nPA.child.process.kill()\n```\n来结束子程序，但一般不推荐这么做，因为这只是单纯结束了子程序，其它关联的内存占用还没被结束，推荐使用 `child.kill()` 或 `child.destroy()` 方法结束子程序。  \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxxxily%2Fprocessassist","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxxxily%2Fprocessassist","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxxxily%2Fprocessassist/lists"}