可能重复:
C#数组是否安全?
我已经为C#编程了10个月了.我现在正在学习多线程,似乎工作得很好.我有多维数组
string[,] test = new string[5, 13];
Run Code Online (Sandbox Code Playgroud)
我有线程调用方法,最终将其输出保存到上面数组内的不同坐标.没有任何一个线程写入与另一个线程相同的位置.
所以thread1可能会写入test[1,10]但没有其他线程会写入test[1,10]
我的问题是:我已经读过关于在我的数组之类的对象上使用锁,我是否必须担心锁,即使我的线程可能同时访问测试数组但从不写入相同的坐标(内存位置) ?
到目前为止,在我的测试中,我没有遇到任何问题,但如果有人比我更熟悉,我知道我可能有问题,那么我会研究使用锁.
对于 python 来说,我发现子进程的 check_output 在 Windows 上运行得很好,但它似乎只运行 Windows PATH 环境变量中的 cmd。
我可以执行以下命令:
import sys
from subprocess import check_output
cmd = check_output("ipconfig", shell=True)
print(cmd.decode(sys.stdout.encoding))
Run Code Online (Sandbox Code Playgroud)
并且 ipconfig 输出显示正常。
如果我尝试运行不在路径中的特定命令并尝试绝对路径,我会收到错误。
import sys
from subprocess import check_output
cmd = check_output("c:\\test\\test.exe", shell=True)
print(cmd.decode(sys.stdout.encoding))
Run Code Online (Sandbox Code Playgroud)
是否无法对 check_output 使用绝对路径引用?我没有找到任何..
我什至尝试更改到该目录..
import sys
from subprocess import check_output
import os
os.chdir("c:\\test\\")
cmd = check_output("test.exe", shell=True)
print(cmd.decode(sys.stdout.encoding))
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误
File "C:\Python35\lib\subprocess.py", line 398, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command 'naviseccli.exe' returned non-zero exit status 1
Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud) 我在 stackoverflow sftp server Implementaion with Python上看到了这篇文章
我尝试设置https://pypi.python.org/pypi/sftpserver,但我似乎无法让它正常工作。
就像上面的 stackoverflow 帖子一样,我正在尝试开发一种将 ssh 连接到交换机的方法,我可以使用 paramiko 来做到这一点,但随后我需要能够将文件/配置从交换机复制回我所在的 Windows 主机运行我的 python 脚本。我需要在我的 Windows 主机上运行一个基于 python 的 sftp 服务器,该服务器将接受来自任何 sftp 客户端(如交换机)的连接。所以我目前使用下面的代码在我的 Windows 主机上以 python 运行 sftp 服务器。我测试了来自另一台Windows主机的连接,我能够很好地远程登录到端口22,所以我知道Python的sftp服务器正在运行,当我从另一台Windows主机运行winscp时,它会建立连接,但随后超时。我可以设置一个 ftp python 服务器并使用 ftp 客户端进行连接,所以我知道我没有任何类型的网络问题。
当我在 python 中运行下面的 sftp 服务器时,不确定我做错了什么?我不知道我的密钥是否错误,我按照上面 sftpserver 链接中的步骤操作并运行“openssl req -out CSR.csr -new -newkey rsa:2048 -nodes -keyout /tmp/test_rsa.key”来创建我拥有的 ubuntu 主机的密钥。当我使用 winscp 进行连接时,我没有看到客户端需要任何用户名或密码。这是来自 Windows 的 netstat -aon 输出,因此它正在侦听 22 并从另一台主机远程登录到它。
活跃连接数
原始本地地址外部地址状态 PID TCP 0.0.0.0:22 0.0.0.0:0 监听 7012
我当前使用的代码取自以下内容: https: //gist.github.com/Girgitt/2df036f9e26dba1baaddf4c5845a20a2 所以简而言之,我需要一个在 …
我见过很多关于C#的混合gui/cli应用程序的例子.我已经实现了这样的应用程序,但是我很难弄清楚如何防止.exe在命令行上运行时,不会立即返回到提示符.
//defines for commandline output
[DllImport("kernel32.dll")]
static extern bool AttachConsole(int dwProcessId);
private const int ATTACH_PARENT_PROCESS = -1;
[STAThreadAttribute]
static void Main(string[] args)
{
// load cli
// redirect console output to parent process;
// must be before any calls to Console.WriteLine()
AttachConsole(ATTACH_PARENT_PROCESS);
if (args.Length == 0)
{
//loads gui
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new form_Main());
}
else
{
cli_main cli = new cli_main();
cli.start_cli(args);
Console.WriteLine("finished");
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
Application.Exit();
}
}
Run Code Online (Sandbox Code Playgroud)
我得到以下输出
C:\Users\john\Documents\Visual Studio 2010\Projects\test\test\test\bin\Debug>test.exe -param1 test
-param2 test2
C:\Users\john\Documents\Visual Studio 2010\Projects\test\test\test\bin\Debug>Output was successful. …Run Code Online (Sandbox Code Playgroud)