python是并发编程的一个重要选项(Is python a serious option for concurrent programming)
只是考虑开始学习python,但在投入更多时间之前,我有一个担心。 让我把这句话作为一种声明加以说明,然后再考虑其他人对此发表评论,因为声明中的假设可能是无效的:
我已阅读关于GIL和共识似乎是如果你需要Python中的并发解决方案,你最好的选择是分叉一个新的过程,以避免GIL。
我担心的是,如果我遇到了问题,我想在N个处理器之间拆分成N * 2个部分(假设例如我有一个服务器运行带有8个内核的* nix o / s),则会产生上下文切换处罚在进程之间而不是在线程之间,这会更加昂贵,这会限制性能。
我问这是因为其他语言在那里声称在这样的场景中表现突出,我想知道Python是否适合这个舞台。
just considering starting to learning python but I have one concern before I invest more time. Let me phrase this as a statement followed by a concern for others to comment on as perhaps the assumptions in the statement are invalid:
I have read about GIL and the consensus seems to be if you require concurrent solutions in python your best bet is to fork a new process to avoid GIL.
My concern is that if I have a problem I'd like to split into N*2 pieces across N processors (assume for example I have a single server running a *nix o/s with say 8 cores) I will incur context switching penalties between processes rather than between threads, which is more costly, which will limit performance.
I ask this because other languages are out there that claim to excel in such a scenario and I wonder is python appropriate for this arena.
原文:https://stackoverflow.com/questions/2150144
最满意答案
你可以通过使用数组作为堆栈来解决这个问题。 下面的代码的评论有一个正确的解释。
function parseTree(string) { // split string into an array // reduce the array into a proper tree where the // first and last item will be the entire tree, hence, // the access of the first index from the last part // of this expression return string.split(/(}|{|\-)/) .reduce(parseTreeReducer, [])[0]; } function parseTreeReducer(array, ch, index, original) { // always track the index of the open bracket // from the array stack let indexBracket = array.lastIndexOf('{'); if(ch === '{') { // is it an open bracket? // store it in the array stack! array.push(ch); } else if(ch === '}') { // is it a close bracket? // remove the last open bracket // this prepares the nodes after the open bracket index // to be the children of the next node array.splice(indexBracket, 1); } else if(ch !== '-' && ch) { // make sure to ignore '-' key // push the node in the array stack array.push({ name: ch, // name // ensure that we only get the nodes that are after the // last open bracket from the array stack and remove them. // These removed nodes will be assigned as children for // this current node children: array.splice( // make sure we don't delete the open bracket indexBracket + 1, // only remove items beyond the open bracket index array.length - indexBracket - 1 ).reverse() // reverse to represent the expected output (optional!) }); } // return the array stack return array; }
function parseTree(string) { // split string into an array // reduce the array into a proper tree where the // first and last item will be the entire tree, hence, // the access of the first index from the last part // of this expression return string.split(/(}|{|\-)/) .reduce(parseTreeReducer, [])[0]; } function parseTreeReducer(array, ch, index, original) { // always track the index of the open bracket // from the array stack let indexBracket = array.lastIndexOf('{'); if(ch === '{') { // is it an open bracket? // store it in the array stack! array.push(ch); } else if(ch === '}') { // is it a close bracket? // remove the last open bracket // this prepares the nodes after the open bracket index // to be the children of the next node array.splice(indexBracket, 1); } else if(ch !== '-' && ch) { // make sure to ignore '-' key // push the node in the array stack array.push({ name: ch, // name // ensure that we only get the nodes that are after the // last open bracket from the array stack and remove them. // These removed nodes will be assigned as children for // this current node children: array.splice( // make sure we don't delete the open bracket indexBracket + 1, // only remove items beyond the open bracket index array.length - indexBracket - 1 ).reverse() // reverse to represent the expected output (optional!) }); } // return the array stack return array; } /* THE CODE BELOW IS ONLY FOR DEMO USAGE */ var input = document.querySelector('input'); var output = document.querySelector('pre'); setOutput(); input.addEventListener('keyup', setOutput); function setOutput() { output.innerHTML = JSON.stringify(parseTree(input.value), 0, 4); }
.as-console-wrapper{min-height:100%;top:0} input{width: 100%} pre{background-color: #ccc; padding: 1em}
<input type="text" autofocus value="{{Axe-Barathrum-Clockwork}{Abaddon-Clinkz-Bane Elemental}Dragon Knight-Bristleback}{Ezalor}Furion" /> <br> <pre></pre>
You can solve this by using arrays as stacks. There's a proper explanation from the comments of the code below.
function parseTree(string) { // split string into an array // reduce the array into a proper tree where the // first and last item will be the entire tree, hence, // the access of the first index from the last part // of this expression return string.split(/(}|{|\-)/) .reduce(parseTreeReducer, [])[0]; } function parseTreeReducer(array, ch, index, original) { // always track the index of the open bracket // from the array stack let indexBracket = array.lastIndexOf('{'); if(ch === '{') { // is it an open bracket? // store it in the array stack! array.push(ch); } else if(ch === '}') { // is it a close bracket? // remove the last open bracket // this prepares the nodes after the open bracket index // to be the children of the next node array.splice(indexBracket, 1); } else if(ch !== '-' && ch) { // make sure to ignore '-' key // push the node in the array stack array.push({ name: ch, // name // ensure that we only get the nodes that are after the // last open bracket from the array stack and remove them. // These removed nodes will be assigned as children for // this current node children: array.splice( // make sure we don't delete the open bracket indexBracket + 1, // only remove items beyond the open bracket index array.length - indexBracket - 1 ).reverse() // reverse to represent the expected output (optional!) }); } // return the array stack return array; }
function parseTree(string) { // split string into an array // reduce the array into a proper tree where the // first and last item will be the entire tree, hence, // the access of the first index from the last part // of this expression return string.split(/(}|{|\-)/) .reduce(parseTreeReducer, [])[0]; } function parseTreeReducer(array, ch, index, original) { // always track the index of the open bracket // from the array stack let indexBracket = array.lastIndexOf('{'); if(ch === '{') { // is it an open bracket? // store it in the array stack! array.push(ch); } else if(ch === '}') { // is it a close bracket? // remove the last open bracket // this prepares the nodes after the open bracket index // to be the children of the next node array.splice(indexBracket, 1); } else if(ch !== '-' && ch) { // make sure to ignore '-' key // push the node in the array stack array.push({ name: ch, // name // ensure that we only get the nodes that are after the // last open bracket from the array stack and remove them. // These removed nodes will be assigned as children for // this current node children: array.splice( // make sure we don't delete the open bracket indexBracket + 1, // only remove items beyond the open bracket index array.length - indexBracket - 1 ).reverse() // reverse to represent the expected output (optional!) }); } // return the array stack return array; } /* THE CODE BELOW IS ONLY FOR DEMO USAGE */ var input = document.querySelector('input'); var output = document.querySelector('pre'); setOutput(); input.addEventListener('keyup', setOutput); function setOutput() { output.innerHTML = JSON.stringify(parseTree(input.value), 0, 4); }
.as-console-wrapper{min-height:100%;top:0} input{width: 100%} pre{background-color: #ccc; padding: 1em}
<input type="text" autofocus value="{{Axe-Barathrum-Clockwork}{Abaddon-Clinkz-Bane Elemental}Dragon Knight-Bristleback}{Ezalor}Furion" /> <br> <pre></pre>
相关问答
更多-
我会检查构造函数属性。 例如 var stringConstructor = "test".constructor; var arrayConstructor = [].constructor; var objectConstructor = {}.constructor; function whatIsIt(object) { if (object === null) { return "null"; } else if (object === undefined ...
-
你正在设置innerHTML ,它设置字符串,而你应该使用appendChild() ,如: function updateName() { var name = document.getElementById('name'); var text = document.createTextNode(sceneData.scene[curLoc].name); name.appendChild(text); } 或者更好地直接设置html,如下: function updateNam ...
-
有多种方式。 这是一个: var ideaBoard = {} $('#data > ul').each(function() { var data = {}, // board data title = $(this).find('.board-title').text(); // board title // find categories $(this).find('> li > .category-title').each(function() { ...
-
取决于你想要的阵列中的哪一个。 这是第一组的输出。 console.log(data[0].componentName); console.log(data[0].liveScore.executionTime); 第二集: console.log(data[1].componentName); console.log(data[1].liveScore.executionTime); 等等... Depends which one in the array you want. Here is the ...
-
JavaScript需要推送或更新更多嵌套数据的json对象(Javascript need to push or update a json object with more nested data)[2022-11-11]
只是做 orderDetails[0].program = newOrder[0].program; 检查这个小提琴 Just make orderDetails[0].program = newOrder[0].program; check this fiddle -
你可以通过使用数组作为堆栈来解决这个问题。 下面的代码的评论有一个正确的解释。 function parseTree(string) { // split string into an array // reduce the array into a proper tree where the // first and last item will be the entire tree, hence, // the access of the first index from the la ...
-
假设你解析了JSON,迭代对象的属性并检查是否有任何属性值是一个对象: var isNested = Object.keys(obj).some(function(key) { return obj[key] && typeof obj[key] === 'object'; }); Assuming you parsed the JSON, iterate over the properties of the object and check whether any property value ...
-
通过Javascript语法从JSON对象读取[重复](Reading from a JSON object via Javascript syntax [duplicate])[2022-07-08]
使用: console.log("first verb: "+ obj[0].srcLanguageSentence.verb.text) 代替: console.log("first verb: "+ ((obj[0].srcLanguageSentence.verb[2]).text) 通常,在访问数组时需要使用括号表示法[]在访问对象的属性时需要使用点表示法( .something )。 在JSON中,数组由[] {}指示,而对象由{}指示。 这两条简单的规则可以很容易地找出如何访问通常在复杂的J ... -
我认为你必须在如何使用ajax方面做错事。 不得不修复粘贴代码中的几个语法错误,但我设法让这个在小提琴中运行。 $.ajax({ type: 'GET', url: 'https://airbly.com/webdevjob/api.php', dataType: 'json', success: function(data, textStatus, jqXHR){ console.log(display_json(data)); } }); fun ...
-
嵌套的JSON对象(Nested JSON object)[2020-12-17]
使用HTML树,您将获得PRODUCT_LIST的以下JSON表示: [ { "CATEGORY_ID":"A", "PRODUCT_DETAILS":[ { "PRODUCT_ID":"A.1", "PRODUCT_NAME":"AAAA1111", "UNIT_COST":0.1 } ] }, { "CATEGORY_ID":"B" ...