python:如何实时获取调用函数的结果?

时间:2018-05-06 12:18:03

标签: python multithreading call

我想要做的是实时获取呼叫功能的结果。 例如,我想实时获取类模型中Type的结果。但是,如果我使用return,我只能获得i一次的结果。

i

任何人都有好主意?希望能得到帮助。

1 个答案:

答案 0 :(得分:0)

你有几种选择;你可以:

  • 使用生成器函数,在迭代时生成结果。这要求var input = `00000000000\n00111111000\n01110011100\n01100000110\n01110011100\n01111111100\n00000000000`, input2 = `00000000000\n00111111000\n01110011100\n01100000000\n01110011100\n01111111100\n00000000000`; function hasLeft1(arr, x, y) { if (y == 0) { return -1; } for (var i = y - 1; i > -1; i--) { if (arr[x][i] == "1" || arr[x][i] == 1) { return i; } } return -1; } function hasRight1(arr, x, y) { if (y == arr[x].length - 1) { return -1; } for (var i = y + 1; i < arr[x].length; i++) { if (arr[x][i] == "1" || arr[x][i] == 1) { return i; } } return -1; } function hasTop1(arr, x, y) { if (x == 0) { return -1; } for (var i = x - 1; i > -1; i--) { if (arr[i][y] == "1" || arr[i][y] == 1) { return i; } } return -1; } function hasBottom1(arr, x, y) { if (x == arr.length - 1) { return -1; } for (var i = x + 1; i < arr.length; i++) { if (arr[i][y] == "1" || arr[i][y] == 1) { return i; } } return -1; } function findLakeZeros(arr) { var output = []; for (var i = 0; i < arr.length; i++) { for (var j = 0; j < arr[i].length; j++) { if (arr[i][j] == "0") { var obj = { left: hasLeft1(arr, i, j), top: hasTop1(arr, i, j), right: hasRight1(arr, i, j), bottom: hasBottom1(arr, i, j) }; if ( obj.top != -1 && obj.right != -1 && obj.bottom != -1 && obj.left != -1 ) output.push({ num: output.length, x: i, y: j, coords: obj }); } } } return output; } function inRowZeros(z1, z2) { if ( z1.x == z2.x && z1.coords.left == z2.coords.left && z1.coords.right == z2.coords.right ) { return true; } return false; } function inColumnZeros(z1, z2) { if ( z1.y == z2.y && z1.coords.top == z2.coords.top && z1.coords.bottom == z2.coords.bottom ) { return true; } return false; } function belongTo(arr, zero) { var keys = Object.keys(arr); for (var i = 0; i < keys.length; i++) { for (var j = 0; j < arr[keys[i]].length; j++) { if ( arr[keys[i]][j].x == zero.x && arr[keys[i]][j].y == zero.y && arr[keys[i]][j].coords.top == zero.coords.top && arr[keys[i]][j].coords.right == zero.coords.right && arr[keys[i]][j].coords.bottom == zero.coords.bottom && arr[keys[i]][j].coords.left == zero.coords.left ) { return keys[i]; } } } return -1; } function mergeZeros(arr) { var obj = {}; for (var i = 0; i < arr.length - 1; i++) { for (var j = i + 1; j < arr.length; j++) { if (inColumnZeros(arr[i], arr[j])) { var itm = belongTo(obj, arr[i]), itm2 = belongTo(obj, arr[j]); if (itm != -1) { obj[itm].push(arr[j]); } else if (itm2 != -1) { obj[itm2].push(arr[i]); } else { obj[arr[i].num] = []; obj[arr[i].num].push(arr[i]); obj[arr[i].num].push(arr[j]); } } else if (inRowZeros(arr[i], arr[j])) { var itm = belongTo(obj, arr[i]), itm2 = belongTo(obj, arr[j]); if (itm != -1) { obj[itm].push(arr[j]); } else if (itm2 != -1) { obj[itm2].push(arr[i]); } else { obj[arr[i].num] = []; obj[arr[i].num].push(arr[i]); obj[arr[i].num].push(arr[j]); } } } } return obj; } function unique(arr) { for (var i = 0; i < arr.length - 1; i++) { for (var j = i + 1; j < arr.length; j++) { if (arr[i].num == arr[j].num) { arr.splice(j, 1); i--; break; } } } return arr; } function areNighbour(z1, z2) { if (z1.x == z2.x && Math.abs(z1.y - z2.y) == 1) { return true; } if (z1.y == z2.y && Math.abs(z1.x - z2.x) == 1) { return true; } return false; } function validLake(arr) { var rows = []; var cols = []; for (var i = 0; i < arr.length; i++) { rows.push(arr[i].x); cols.push(arr[i].y); } rows.sort(); cols.sort(); for (var i = 0; i < rows.length - 1; i++) { if (rows[i] == rows[i + 1]) { continue; } else if (rows[i] != rows[i + 1] - 1) { return false; } } for (var i = 0; i < cols.length - 1; i++) { if (cols[i] == cols[i + 1]) { continue; } else if (cols[i] != cols[i + 1] - 1) { return false; } } return true; } function starter(input) { var zeros = findLakeZeros(input.split("\n")); var result = mergeZeros(zeros); var oo = Object.keys(result); for (var i = 0; i < oo.length; i++) { var un = unique(result[oo[i]]); if (validLake(un)) { console.log("These zeros are create a lake: ", un); } } } starter(input); console.log("--------------example 2-------------"); starter(input2);调用在model1.func()调用返回的生成器上进行model1.func()调用。如果您不需要访问其他线程中的数据,请使用此选项。

  • 使用队列;在生成队列时将i结果推送到队列中,另一个线程可以从队列中接收它们。