[英]Using multiple programs simultaneously in Python
I'm fairly new to Python and I'm trying to write a script to automate a test. 我对Python还是很陌生,我正在尝试编写一个脚本来自动化测试。
How it works: Program A: Sends commands through serial port waits for response and then executes next command Program B: Uses a TCP_Client.exe app using Subprocess module to send arguments (ip, port, number of packets, size) along with the app to the command prompt and reads output. 工作原理:程序A:通过串行端口发送命令以等待响应,然后执行下一条命令。程序B:使用带有Subprocess模块的TCP_Client.exe应用程序与该应用程序一起发送自变量(ip,端口,数据包数量,大小)到命令提示符并读取输出。
What its supposed to do: 它应该做什么:
RunSer2Command(lines2[21])
time.sleep(1)
ls_output = subprocess.Popen(['tcpclient.exe','192.168.22.33','5000','100000','1400'],stdout=subprocess.PIPE)
time.sleep(1)
RunSer2Command(lines2[22])
RunSer2Command(lines2[23])
time.sleep(1)
ls_output = subprocess.Popen(['3'],stdout = subprocess.PIPE)
ls_output.communicate()
RunSer2Command(lines2[24])
ser2.close()
Something Like this 像这样
Could someone tell me if I should read up multi threading or is this too small to require it? 有人可以告诉我是否应该阅读多线程,或者它太小而不能要求吗? If so what should I be looking for?
如果是这样,我应该找什么?
Thanks in advance 提前致谢
Answering my own question. 回答我自己的问题。 Short answer is Threading will do it.
简短的答案是线程可以做到。 It is also unnecessary.
这也是不必要的。 The subprocess module has enough for me to make it work.
子流程模块足以让我正常工作。 I just wasn't doing it right
我只是做得不好
RunSer2Command(lines2[21])
time.sleep(1)
ls_output = subprocess.Popen(['tcpclient.exe','192.168.4.110','8000','10000','1400'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,bufsize=3)
time.sleep(2)
RunSer2Command(lines2[22])
RunSer2Command(lines2[23])
time.sleep(1)
ls_output.communicate(input = '3')
ls_output.wait()
RunSer2Command(lines2[24])
For those who care threading route did get me to a certain point and I'll add that but I didn't go to the last condition since well... I found the easier route 对于那些在意穿线路线的人确实使我到达了某个点,我会补充一点,但自那以后我并没有走到最后一个条件...我发现了更简单的路线
def start_tcp_client(cond):
ls_output = subprocess.Popen(['tcpclient.exe','192.168.4.110','8000','1000','1400'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,bufsize=3)
with cond:
cond.wait()
ls_output.communicate(input = '3')
ls_output.communicate()
def TCPSettings(cond):
with cond:
RunSer2Command(lines2[22])
RunSer2Command(lines2[23])
cond.notify()
condition = threading.Condition()
condition1 = threading.Condition()
Client_thread=threading.Thread(name='Client_thread', target=start_tcp_client, args=(condition,))
TCP_thread=threading.Thread(name='TCP_thread', target=TCPSettings, args=(condition,))
RunSer2Command(lines2[21])
time.sleep(2)
Client_thread.start()
time.sleep(2)
TCP_thread.start()
time.sleep(1)
Client_thread.join()
time.sleep(10)
RunSer2Command(lines2[24])
I still havent managed to work out all the kinks. 我仍然没有设法解决所有问题。 There appear to be some timing issues.
似乎存在一些计时问题。 I'll update it once I get it working perfectly.
一旦它可以正常运行,我将对其进行更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.