Вы можете использовать multiprocessing.Process
для выполнения именно этого.
Код
import multiprocessing
import time
# bar
def bar():
for i in range(100):
print "Tick"
time.sleep(1)
if __name__ == '__main__':
# Start bar as a process
p = multiprocessing.Process(target=bar)
p.start()
# Wait for 10 seconds or until process finishes
p.join(10)
# If thread is still active
if p.is_alive():
print "running... let's kill it..."
# Terminate
p.terminate()
p.join()
Попробуйте:
Array.prototype.findKey = function(key){
let res = [];
function traverseChildren(input, lookFor) {
//By default, imagine what we are looking at is JSON...
let children = Object.keys(input);
//... however, if it is an array, just stick with that.
if (Array.isArray(input)){
children = input;
}
//Go through everything in the target
for (i = 0; i < children.length; i++) {
//Have we found the *key* we are looking for?
if (children[i] === lookFor && Array.isArray(input)) {
//If so, record the value.
res.push(input[lookFor]);
} else {
//If not, keep searching.
traverseChildren(input[children[i]]);
}
}
}
traverseChildren(this, key);
return res;
}
Когда вы анализируете файл JSON, он ведет себя так же, как обычный объект Javascript, затем:
myJSONfile.feed.entry[index].content.properties.ResourceType.element.Label.__text
Обратите внимание на индекс записи, к которой вы получаете доступ:
myJSONfile.feed.entry[index]
const json = {
"feed": {
"id": "[redacted]",
"title": "",
"updated": "2019-01-17T21:01:03Z",
"entry": [
{
"id": "Web/Lists(guid'[redacted]')/Items(1)",
"category": {
"_term": "[redacted]",
"_scheme": "[redacted url]"
},
"link": {
"_rel": "edit",
"_href": "Web/Lists(guid'[redacted]')/Items(1)"
},
"title": "",
"updated": "2019-01-17T21:01:03Z",
"author": {
"name": ""
},
"content": {
"properties": {
"ResourceType": {
"element": {
"Label": {
"__prefix": "d",
"__text": "Guides & Protocols"
},
"TermGuid": {
"__prefix": "d",
"__text": "[redacted]"
},
"WssId": {
"_m:type": "[redacted]",
"__prefix": "d",
"__text": "706"
},
"__prefix": "d"
},
"_m:type": "Collection([redacted])",
"__prefix": "d"
},
"__prefix": "m"
},
"_type": "application/xml"
},
"_m:etag": "\"2\""
}
]
}
}
console.log(json.feed.entry[0].content.properties.ResourceType.element.Label.__text)
// Guides & Protocols