Comments (11)
@chsnt the problem isn't the Translate client, I believe it's this code:
getFiles(dir, function (err, files) {
pbSubr.start(files.length, 0);
files.forEach( file => {
fs.pathExists(file.replace('data\\boards\\', 'data\\boards-ru\\'))
.then(exists => {
if(!exists) {
translatorWithErrorHandle (file);
} else {
pbSubr.increment()
pbSubr.update(undefined, {dir: file.split('\\')[3]});
}
})
});
});
This is going to open all files in parallel, so if there are many files in dir
you might hit a maximum amount of concurrent open files at the operating system level.
I would potentially rewrite this like:
getFiles(dir, async function (err, files) {
pbSubr.start(files.length, 0);
for (const file of files) {
const exists = await fs.pathExists(file.replace('data\\boards\\', 'data\\boards-ru\\'))
. if(!exists) {
await translatorWithErrorHandle (file);
} else {
pbSubr.increment()
await pbSubr.update(undefined, {dir: file.split('\\')[3]});
}
})
}
});
☝️ this is pseudo code that would translate a file one at a time, assuming that pbSubr.update
returns a promise.
As an alternative to async/await
you might also look at p-queue which allows you to control the amount of concurrency happening in your program.
In general, you don't want to perform a large amount of async work in a loop, because you'll create a lot of open handles at once.
from nodejs-translate.
@chsnt this indicates that there are too many open file handles in your application, is there a chance you are initializing the client in a loop? Could you share a snippet of the code you are running?
from nodejs-translate.
main.js
require('dotenv').config({ debug: process.env.GOOGLE_APPLICATION_CREDENTIALS })
const translate = require('./translate');
...
translate (text, targetLang)
...
translate.js
const {Translate} = require('@google-cloud/translate').v2;
const tr = (text, target) => {
return new Promise ((resolve, reject)=>{
translate.translate(text, target)
.then(([result])=>resolve(result))
.catch(err=>{
console.error(err);
reject(err)
})
})
}
module.exports = tr;
from nodejs-translate.
@chsnt what does the following do:
require('dotenv').config({ debug: process.env.GOOGLE_APPLICATION_CREDENTIALS })
I could you try running translate.js
in isolation, with GOOGLE_APPLICATION_CREDENTIALS
set to the path of your credentials file?
from nodejs-translate.
I could you try running translate.js in isolation, with GOOGLE_APPLICATION_CREDENTIALS set to the path of your credentials file?
i run this
require('dotenv').config({ debug: process.env.GOOGLE_APPLICATION_CREDENTIALS })
const {Translate} = require('@google-cloud/translate').v2;
const tr = (text, target) => {
return new Promise ((resolve, reject)=>{
translate.translate(text, target)
.then(([result])=>resolve(result))
.catch(err=>{
console.error(err);
reject(err)
})
})
}
tr('hello', 'ru')
.then(res => console.log(res))
.catch(err => console.log(err))
and it works correctly.
But when i run it without this string
require('dotenv').config({ debug: process.env.GOOGLE_APPLICATION_CREDENTIALS })
i geting this error
Error: Unable to detect a Project Id in the current environment.
from nodejs-translate.
try:
const {Translate} = require('@google-cloud/translate').v2;
const translate = Translate({projectId: 'your-project-id'})
const tr = (text, target) => {
return new Promise ((resolve, reject)=>{
translate.translate(text, target)
.then(([result])=>resolve(result))
.catch(err=>{
console.error(err);
reject(err)
})
})
}
tr('hello', 'ru')
.then(res => console.log(res))
.catch(err => console.log(err))
Providing GOOGLE_APPLICATION_CREDENTIALS
with a path to your credentials, rather than using dotenv
.
dotenv
simply loads a local .env
file, with environment variables, I just want to eliminate this as a potential cause of your issues.
from nodejs-translate.
In my system environment variables
GOOGLE_APPLICATION_CREDENTIALS=d:\Projects\translator\translator-22f1b569e00b.json
- translator-22f1b569e00b.json is my key-file (in
.env
-file : GOOGLE_APPLICATION_CREDENTIALS=D:\Projects\translator\translator-22f1b569e00b.json )
i try this code:
const {Translate} = require('@google-cloud/translate').v2;
const translate = Translate({projectId: 'your-project-id'})
const tr = (text, target) => {
return new Promise ((resolve, reject)=>{
translate.translate(text, target)
.then(([result])=>resolve(result))
.catch(err=>{
console.error(err);
reject(err)
})
})
}
tr('hello', 'ru')
.then(res => console.log(res))
.catch(err => console.log(err))
and got error:
Error: Unable to detect a Project Id in the current environment.
at D:\Projects\translator\node_modules\google-auth-library\build\src\auth\googleauth.js:90:31
at processTicksAndRejections (internal/process/task_queues.js:93:5)
from nodejs-translate.
@chsnt could you also try setting the environment variable GCLOUD_PROJECT
to the name of the project the GOOGLE_APPLICATION_CREDENTIALS
.
I'm not sure why dotenv
would not be throwing the same error, but I believe you might find setting this environment variable explicitly will do the trick.
from nodejs-translate.
@bcoe
Sory, i try it again
const {Translate} = require('@google-cloud/translate').v2;
const translate = Translate({projectId: 'your-project-id'})
const tr = (text, target) => {
return new Promise ((resolve, reject)=>{
translate.translate(text, target)
.then(([result])=>resolve(result))
.catch(err=>{
console.error(err);
reject(err)
})
})
}
tr('hello', 'ru')
.then(res => console.log(res))
.catch(err => console.log(err))
and it works correctly (for single use).
But when i use this code like a module for multiple call i had error again:
EMFILE: too many open files, open 'd:\Projects\translator\translator-22f1b569e00b.json'
[Error: EMFILE: too many open files, open 'd:\Projects\translator\translator-22f1b569e00b.json'] {
errno: -4066,
code: 'EMFILE',
syscall: 'open',
path: 'd:\\Projects\\translator\\translator-22f1b569e00b.json'
}
(i dont use dotenv
anymore)
from nodejs-translate.
@chsnt could you provide code that demonstrates how you are running this logic multiple times; my hunch is that there's a chance you're creating a Translate
client in a loop. Make sure you only create a single translate
instance, and then call this multiple times (rather than creating a new translate
client for each request.
from nodejs-translate.
@bcoe
Thanks for an answer
I use translate like this:
const translateG = require('./translate');
...
translateG (text, targetLang) // multiple call (its wrong?)
...
(full code https://gist.github.com/chsnt/0d669b0a9509086dce829f8d0ae7257c )
translate.js
const {Translate} = require('@google-cloud/translate').v2;
const translate = Translate({projectId: 'your-project-id'})
const tr = (text, target) => {
return new Promise ((resolve, reject)=>{
translate.translate(text, target)
.then(([result])=>resolve(result))
.catch(err=>{
console.error(err);
reject(err)
})
})
}
module.exports = tr;
from nodejs-translate.
Related Issues (20)
- translate_translate_text_with_glossary: should translate text with a glossary in project failed
- translate_translate_text_with_glossary_and_model: should translate text with a glossary and Automl model in project failed
- translate_translate_text_with_model: should translate text with an automl model in project failed
- translate_batch_translate_document: "before all" hook for "should batch translate the input documents" failed
- translate_batch_translate_text_beta: "after each" hook for "should batch translate the input text" failed
- translate_create_glossary_beta: should create a glossary failed
- translate_create_glossary_beta: "after all" hook: cleanup for glossary create for "should create a glossary" failed
- translate_delete_glossary_beta: "before all" hook for "should delete a glossary" failed
- translate_detect_language_beta: should detect the language of the input text failed
- translate_get_glossary_beta: "before all" hook for "should get a glossary" failed
- translate_get_glossary_beta: "after all" hook for "should get a glossary" failed
- translate_list_codes_beta: should list available language codes failed
- translate_list_glossary_beta: "before all" hook for "should list glossaries in project" failed
- translate_list_glossary_beta: "after all" hook for "should list glossaries in project" failed
- translate_list_language_names_beta: should list available language names failed
- translate_translate_document: should translate the input document failed
- translate_translate_text_with_glossary_beta: "before all" hook for "should translate text with a glossary in project" failed
- translate_translate_text_with_glossary_beta: "after all" hook for "should translate text with a glossary in project" failed
- [Policy Bot] found one or more issues with this repository.
- [Policy Bot] found one or more issues with this repository.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nodejs-translate.