Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 剧本完成了吗?应该检查一下我是初学者_Python_Linux_Multithreading - Fatal编程技术网

Python 剧本完成了吗?应该检查一下我是初学者

Python 剧本完成了吗?应该检查一下我是初学者,python,linux,multithreading,Python,Linux,Multithreading,我只是python的初学者。我试图实现的是制作两个线程并在不同的线程中调用不同的函数。我让线程1中的函数执行一个函数60秒,线程2同时执行并等待主线程等待70秒。当线程1退出时,它也应该退出第二个线程最后,控制应该到达主线程,对线程1和线程2的调用应该再次进行,并重复相同的过程。 我试着用下面的线程实现它,但我觉得我做不到 I have made a script in which i have started two thread named thread 1 and thread 2. 在

我只是python的初学者。我试图实现的是制作两个线程并在不同的线程中调用不同的函数。我让线程1中的函数执行一个函数60秒,线程2同时执行并等待主线程等待70秒。当线程1退出时,它也应该退出第二个线程最后,控制应该到达主线程,对线程1和线程2的调用应该再次进行,并重复相同的过程。 我试着用下面的线程实现它,但我觉得我做不到

I have made a script in which i have started two thread named thread 1 and thread 2.
在线程1中,一个函数将名为func1运行,在线程2中,函数2将名为func 2运行。 线程1将执行一个命令并等待60秒。 线程2将只运行到线程1运行为止。 在这之后,同样的过程在中断80秒后继续进行

我是python的初学者。 请说明我做错了什么,以及如何改正

#!/usr/bin/python

import threading
import time
import subprocess
import datetime
import os
import thread

thread.start_new_thread( print_time, (None, None))
thread.start_new_thread( print_time1, (None, None))
command= "strace -o /root/Desktop/a.txt -c ./server"
final_dir = "/root/Desktop"
exitflag = 0
# Define a function for the thread
def print_time(*args):
    os.chdir(final_dir)
    print "IN first thread"
    proc = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    proc.wait(70)
    exitflag=1

def print_time1(*args):
    print "In second thread"
    global exitflag
    while exitflag:
        thread.exit()
        #proc = subprocess.Popen(command1,shell=True,stdout=subprocess.PIPE, sterr=subprocess.PIPE)



# Create two threads as follows
try:
    while (1):
        t1=threading.Thread(target=print_time)
        t1.start()
        t2=threading.Thread(target=print_time1)
        t2=start()
        time.sleep(80)
        z = t1.isAlive()
        z1 = t2.isAlive()
        if z:
            z.exit()
        if z1:
            z1.exit()
           threading.Thread(target=print_time1).start()
           threading.Thread(target=print_time1).start()
        print "In try"
except:
   print "Error: unable to start thread"

我无法运行该示例,我需要将函数定义更改为

def print_time(*args)
线程调用

thread.start_new_thread( print_time, (None, None))
那你就有很多问题了

  • 您当前没有等待在第二个线程中设置exitflag,它只是运行到完成
  • 要在线程之间共享变量,您需要在线程中声明它们为全局变量,否则会得到一个局部变量
  • print\u time1
    函数中的thread.exit()生成错误
  • 问题描述和代码中的计时不匹配
  • 因此,要解决打印时间1的问题1-3,请声明如下(从末尾删除退出)

    <>但是,检查DOC的线程模块(),[…]但是,您应该考虑使用高级线程模块来代替。
    关于代码的最后一个问题是,在启动新线程之前,您应该检查线程是否已经完成。现在,每80秒启动两个新线程,这与旧线程是否已运行完成无关。除非这是想要的行为,否则我会在while循环中添加一个检查。此外,在执行此操作时,将try子句移动到尽可能靠近可能引发异常的位置,即创建线程的位置。您现在尝试封装while循环的方式并不常见,而且我也不是很pythonic(增加了代码的复杂性)

    请尝试提出一个更接近您实际问题的标题,谢谢!检查此链接。它将有助于Python线程。你能分享你的邮件id或发送测试邮件到gunjanmehta08ATgmaildotcomI吗我也用最新的代码更新了问题你能检查一下吗
    def print_time1(*args):
        global exitflag
        while exitflag == 0:  # wait for print_time 
            next
        # Do stuff when thread is finalizing
    
    import threading
    ...
    
    while(1):
        threading.Thread(target=print_time).start()
        threading.Thread(target=print_time1).start()
        time.sleep(80)