我的 Rust 代码如下所示。
#[tokio::main]
pub async fn main() {
for i in 1..10 {
tokio::spawn(async move {
println!("{}", i);
});
}
}
Run Code Online (Sandbox Code Playgroud)
运行代码时,我希望它以随机顺序打印 1 到 10。但它只是打印一些随机数:
1
3
2
终端将被任务重复使用,按任意键关闭它。
为什么会发生这种情况?
您好,我有以下名为 a.exp 的期望脚本
#!/usr/bin/expect
spawn cat a.txt
Run Code Online (Sandbox Code Playgroud)
其中 a.txt 包含以下单行字符串文本
你好世界
接下来我使用以下命令使其可执行
chmod +x a.exp
Run Code Online (Sandbox Code Playgroud)
现在,我按如下方式运行它
./a.exp
Run Code Online (Sandbox Code Playgroud)
我得到的输出如下
spawn cat a.txt
Run Code Online (Sandbox Code Playgroud)
另一方面,如果我使用以下脚本
puts [exec cat a.txt]
Run Code Online (Sandbox Code Playgroud)
代替
spawn cat a.txt
Run Code Online (Sandbox Code Playgroud)
它确实打印 a.txt 文件的内容。请问你能帮我使用spawn来执行它吗?谢谢!
我有两个gen_server模块.
第一个serv.erl
-module(serv).
-behaviour(gen_server).
-export([init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
code_change/3,
terminate/2,
start_link/0
]).
start_link() ->
gen_server:start_link(?MODULE, [], []).
init([]) ->
process_flag(trap_exit, true),
spawn_link(user, start_link,[]),
{ok, []}.
handle_call(_E, _From, State) ->
{noreply, State}.
handle_cast(_Message, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
handle_info(Message, State) ->
{noreply, State}.
code_change(_OldVersion, State, _Extra) ->
{ok, State}.
Run Code Online (Sandbox Code Playgroud)
和user.erl(除了init/1完全相同):
init([]) ->
{ok, []}.
Run Code Online (Sandbox Code Playgroud)
我以为服务器会永远存在.如果第一台服务器死了,另一台服务器会收到{'EXIT',Pid,Reason}消息.
但是如果你通过serv:start_link()启动模块,用户模块将在启动后立即退出,并显示消息{'EXIT',Pid,normal}.为什么用户会死?
我从tcl脚本中生成了多个sh(sipp)脚本.我想知道这些脚本将并行运行还是作为子进程运行?因为我想并行运行它.
如果我使用线程扩展,那么我是否需要使用任何其他包?
提前致谢.
我有一个init函数,我称之为start_server:
start_server() ->
spawn_link(game_loop(0,0)).
Run Code Online (Sandbox Code Playgroud)
目的是启动一个新进程,开始循环并等待某人在那里发送消息:
game_loop(X,Y) ->
receive
{move, left} ->
tell_client(X+1,Y),
game_loop(X+1,Y);
{move, right} ->
tell_client(X-1,Y),
game_loop(X-1,Y)
end.
Run Code Online (Sandbox Code Playgroud)
我的原因是start_server会返回Pid,这样我就可以在Erlang终端中写出这样的东西:
> Server = server:start_server().
Run Code Online (Sandbox Code Playgroud)
然后使用变量Server通过以下功能处理服务器:
move_left(Pid) ->
Pid ! {move, left}.
Run Code Online (Sandbox Code Playgroud)
但是这不起作用,因为start_server()永远不会返回,为什么呢?
我有一个在nodejs 中使用spawn 创建的进程,并带有选项,shell:true
因此该进程在真实的shell 中启动。因此,当我尝试终止这个进程时,streamingTask.kill()
它不起作用。没有该选项shell:true
一切正常。
这就是我的代码的样子:
var options = {shell:true};
streamingTask = spawn('gst-launch-1.0',args,options);
...
streamingTask.kill()
Run Code Online (Sandbox Code Playgroud)
那么我现在怎样才能杀死这个进程呢?
我正在尝试从以python编写的秤流式传输输出。该程序(scale.py)连续运行并每半秒打印一次原始值。
import RPi.GPIO as GPIO
import time
import sys
from hx711 import HX711
def cleanAndExit():
print "Cleaning..."
GPIO.cleanup()
print "Bye!"
sys.exit()
hx = HX711(5, 6)
hx.set_reading_format("LSB", "MSB")
hx.reset()
hx.tare()
while True:
try:
val = hx.get_weight(5)
print val
hx.power_down()
hx.power_up()
time.sleep(0.5)
except (KeyboardInterrupt, SystemExit):
cleanAndExit()
Run Code Online (Sandbox Code Playgroud)
我正在尝试通过打印一个单独的NodeJs程序(位于同一文件夹中的index.js)获取每个原始数据点print val
。这是我的节点程序。
var spawn = require('child_process').spawn;
var py = spawn('python', ['scale.py']);
py.stdout.on('data', function(data){
console.log("Data: " + data);
});
py.stderr.on('data', function(data){
console.log("Error: " + data);
});
Run Code Online (Sandbox Code Playgroud)
当我运行时sudo node index.js
,没有输出,程序将永久保存。
我的想法是print val
将输出放入stdout流中,并 …
我试图在Y轴上逐个产生+0.6空间的对象.对象应该是0.6,1.2,1.8,2.4,3等,而它看起来像0.6,1.8,3.6,6,9等.我不知道发生了什么,所以我希望你能帮助我,这是一个代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour {
public GameObject cubeOne, cubeDouble, cubeTriple, cubeQuattro, cubeOneShort, cubeDoubleShort, cubeTripleShort, cubeQuattroShort, sphereOne, sphereDouble, sphereTriple, sphereQuattro, sphereOneShort, sphereDoubleShort, sphereTripleShort, sphereQuattroShort;
int whatToSpawn;
float position;
int yRotation;
void Update () {
whatToSpawn = Random.Range(1, 5);
position += 0.6f;
Vector3 newPosition = transform.position;
newPosition.y += position;
switch (whatToSpawn)
{
case 1:
Instantiate(cubeOne, transform.position = newPosition, transform.rotation * Quaternion.Euler(90, 0, 0));
Debug.Log(position);
break;
case 2:
Instantiate(cubeDouble, transform.position = newPosition, transform.rotation * Quaternion.Euler(90, …
Run Code Online (Sandbox Code Playgroud) spawn ×8
erlang ×2
node.js ×2
tcl ×2
async-await ×1
asynchronous ×1
c# ×1
expect ×1
gen-server ×1
object ×1
python ×1
rust ×1
rust-tokio ×1
shell ×1
stdout ×1