我正在尝试使用管道制作一个简单的程序。程序必须请求父级中的整数数组,并将其发送给子级,子级必须对数组进行排序并将其发送回父级。问题是,在通过管道发送后,我不确定如何读取孩子中的数组值。
按照我的代码:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main() {
int fd[2];
pid_t pid;
/* Verifica se ocorreu erro (-1) ao criar o PIPE */
if(pipe(fd) == -1){
perror("pipe");
exit(1);
}
/* Verifica se o fork foi realizado com sucesso > 0 */
if((pid = fork()) < 0){
perror("fork");
exit(1);
}
if(pid > 0){
printf("-----------------------------------------------\n");
printf(" PROCESSO PAI \n");
printf("-----------------------------------------------\n");
/* Como vamos ESCREVER do lado do PAI, fechamos a LEITURA */
close(fd[0]);
int numeros[5];
for (int i = …Run Code Online (Sandbox Code Playgroud) 我在下面有一个简单的代码片段来演示这个问题。
from multiprocessing import Pipe
import time
recv_end, send_end = Pipe(duplex=False)
d = {'word'+str(elem): elem for elem in range(3000)}
start_time = time.time()
send_end.send(d)
print('--- %s seconds ---' % (time.time()-start_time))
Run Code Online (Sandbox Code Playgroud)
以上工作正常,对我的目的来说足够快,没问题。但是如果我将大小设置为 5000,它就会无限期地挂起:
from multiprocessing import Pipe
import time
recv_end, send_end = Pipe(duplex=False)
d = {'word'+str(elem): elem for elem in range(5000)} # changed to 5000
start_time = time.time()
send_end.send(d)
print('--- %s seconds ---' % (time.time()-start_time))
Run Code Online (Sandbox Code Playgroud)
管道是否有大小限制,或者这是一个不可重现的问题?如果你把尺寸做得更大呢?如果有大小限制,避免这个问题并通过管道发送大字典的最佳方法是什么?提前致谢!
事情就是这样......我在我的应用程序中使用了一个 angular 4 模板,它有一个很好的工作翻译服务。问题是我不知道如何在代码中使用该管道。
在 HTML 中<span>{{ this.variable | traslate }}</span>,然后,该服务会转到一些 JSON 文件以查找文本并进行翻译。
这是我的 component.ts 中的代码
const SMALL_WIDTH_BREAKPOINT = 960;
@Component({
selector: 'app-mycomponent',
templateUrl: './mycomponent.component.html',
styleUrls: ['./mycomponent.component.scss']
})
export class TrackingComponent implements OnInit {
currentLang = 'es';
private mediaMatcher: MediaQueryList = matchMedia(`(max-width:
${SMALL_WIDTH_BREAKPOINT}px)`);
constructor(
public translate: TranslateService,
zone: NgZone,
private router: Router,
) {
const browserLang: string = translate.getBrowserLang();
translate.use(browserLang.match(/en|es/) ? browserLang : 'es');
this.mediaMatcher.addListener(mql => zone.run(() => {
this.mediaMatcher = mql;
}));
translate.use(this.currentLang);
}
ngOnInit() { } …Run Code Online (Sandbox Code Playgroud) 假设我得到一个带有特殊字符的字符串,我想使用过滤器/管道来更改它。每个单词的第一个字母也应该大写。
例如"@!? test stri&!ng?"将成为"Test String".
那怎么办呢?
我想将命令的输出通过管道传输到 awk。我想将该数字添加到现有 .txt 文件中新列的每一行。新列应该在最后,不一定是第 2 列。
$命令 1
4512438
Run Code Online (Sandbox Code Playgroud)
$输入.txt
A
B
C
D
Run Code Online (Sandbox Code Playgroud)
$desired_output.txt
A 4512438
B 4512438
C 4512438
D 4512438
Run Code Online (Sandbox Code Playgroud)
我想我需要按照以下方式做一些事情。我不确定如何指定管道进入新列 - 这个 awk 命令只会将整数添加到列中。
$命令1 | awk -F, '{$(NF+1)=++i;}1' OFS=, input.txt > desired_ouput.txt
在这种情况下,我对代码流有点困惑。请参考下面的代码。
import { fromEvent } from 'rxjs';
import { mapTo } from 'rxjs/operators';
let btn = document.getElementById("btnclick");
let btn_clicks = fromEvent(btn, 'click');
let positions = btn_clicks.pipe(mapTo ("Testing MapTo"));
positions.subscribe(x => console.log(x));
Run Code Online (Sandbox Code Playgroud)
Testing MapTo当我单击按钮时它会输出。一开始我认为每当按钮点击发生时,该.pipe()方法被调用,然后该.subscribe()方法被调用导致它在控制台中输出。然而我读到,
subscribe is for activating the observable and listening for emitted values
Run Code Online (Sandbox Code Playgroud)
在链接中。
问题:
this.http.get('url')
.pipe(
filter(x => x % 2 === 0), …Run Code Online (Sandbox Code Playgroud) 我有这个脚本,它根据某些自助餐厅查找文件并使用 Write-Host 写入它们的路径。现在,我想通过管道传输所有这些文件路径,并对这些文件进行处理。
基本上第一个脚本是这样的:
FindFiles.ps1:
Write-Host "C:\SomeDir\file1.txt"
Write-Host "D:\OtherDir\file2.txt"
Run Code Online (Sandbox Code Playgroud)
现在我想做这样的事情:
FindFiles.ps1 | foreach { Copy-Item $_ c:\backup\ }
Run Code Online (Sandbox Code Playgroud)
但显然这是行不通的。(实际上我想做一些比复制文件夹更复杂的事情,但这是另一个问题:-))
加载我正在编写的 R 包时遇到以下错误。
Error in nations %>% rvest::html_nodes(".x") %>% rvest::html_nodes(".y") %>% :
could not find function "%>%"
Run Code Online (Sandbox Code Playgroud)
我不确定如何将它导入到我的 R 包中。这就是我的功能设置方式
nations_url_odd<-nations %>%
rvest::html_nodes('.x') %>%
rvest::html_nodes('.y') %>%
rvest::html_nodes('a')
Run Code Online (Sandbox Code Playgroud) 我不确定这是微不足道的还是对整个 pipe() 函数的错误理解。这是我的问题的最小版本。
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
int main(void) {
int socket[2], r, buff[512];
const char* msg = "what is wrong with pipes";
r = pipe(socket);
if(r < 0) perror("pipe()");
r = write(socket[0], msg, sizeof(*msg));
if(r < 0) perror("write()");
r = read(socket[1], buff, sizeof(*msg));
if(r < 0) perror("read()");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个程序在 Linux 下编译时总是生成以下输出,在 gcc 9.2.1
write(): Bad file descriptor
read(): Bad file descriptor
Run Code Online (Sandbox Code Playgroud)
这里的代码问题究竟是什么?我知道它应该用于具有多个进程的 IPC 设置。但是,为什么这段代码不起作用?我在阅读 pipe 和 pipe2 上的 man 时没有看到任何明显的错误,写和读。
我需要形成各种命令的管道。管道的某些元素或元素序列仅在某些条件成立时才相关。现在,我可以写:
if [[ $whatever ]]; then
cmd1 | cmd2 | cmd3 | cmd4
else
cmd1 | cmd4
fi
Run Code Online (Sandbox Code Playgroud)
但这意味着重复cmd1and cmd4,另外,可能有几个条件,我不想写嵌套的 if 。所以,我试着写这个:
if [[ $whatever ]]; then
pipeline_segment="| cmd2 | cmd3"
else
pipeline_segment=""
fi
cmd1 ${pipeline_segment} | cmd4
Run Code Online (Sandbox Code Playgroud)
但是 - 管道符号没有被解释为使用管道的指令。
我如何让 bash 也执行我想要的管道?
注意:您可以假设 bash 版本为 4 或更高,但前提是您必须这样做。
pipe ×10
angular ×3
c ×2
typescript ×2
arrays ×1
awk ×1
bash ×1
cat ×1
dictionary ×1
fork ×1
javascript ×1
magrittr ×1
observable ×1
pipeline ×1
powershell ×1
python ×1
r ×1
r-package ×1
rxjs ×1
subscribe ×1
translate ×1
write-host ×1