quanyechavs huo
quanyechavs huo

Reputation: 135

how to execute different system commands in differrent threads with python3

I want to execute two different commands in different thread with python3,below is my code:

import time
import threading
import os

class MyThread(threading.Thread):

    def __init__(self, func, args, name=''):
        threading.Thread.__init__(self)
        self.name = name
        self.func = func
        self.args = args

    def run(self):
        self.result = self.func(*self.args)

    def get_result():
        return self.result


def sniffdata1():
    while 1:
        time.sleep(1)
        os.system("echo 2")

def sniffdata2():
        time.sleep(1)
        os.system("echo 1")

sniffThread1=MyThread(sniffdata1,())
sniffThread2=MyThread(sniffdata2,())
sniffThread1.start()
sniffThread2.start()
sniffThread1.join()
sniffThread2.join()

But I can not get my 121212...,the result is 1 2 2 2 2 2 2 2 ...(no more 1),but alway echo 2,can someone help me?

Upvotes: 1

Views: 1301

Answers (2)

Alex Jane
Alex Jane

Reputation: 66

If you want get more 1, you should add while to sniffdata2():

def sniffdata2():
    while 1:
        time.sleep(1)
        os.system("echo 1")

But if you want to get 12121212... all the times, you should use this code:

import time
import threading
import os
flag2 = False;
flag1 = True;

class MyThread(threading.Thread):

    def __init__(self, func, args, name=''):
        threading.Thread.__init__(self)
        self.name = name
        self.func = func
        self.args = args

    def run(self):
        self.result = self.func(*self.args)

    def get_result(self):
        return self.result


def sniffdata1():
    global flag2, flag1
    while 1:
        if flag1:
            flag2 = False
            time.sleep(1)
            os.system("echo 1")
            flag1 = False
            flag2 = True

def sniffdata2():
    global flag2, flag1
    while 1:
        if flag2:
            flag1 = False
            time.sleep(1)
            os.system("echo 2")
            flag2 = False
            flag1 =True

sniffThread1=MyThread(sniffdata1,())
sniffThread2=MyThread(sniffdata2,())
sniffThread1.start()
sniffThread2.start()

Upvotes: 1

quanyechavs huo
quanyechavs huo

Reputation: 135

When I use multi process to deal with it,it works,but I am not very clear about the reason,below is my solution code:

from multiprocessing import Process
import os
import time

def worker1():
  """test python multi process"""
  while 1:
      time.sleep(1)
      os.system("echo 1")
      #os.system("ping www.baidu.com")

def worker2():
  """test python multi process"""
  while 1:
      time.sleep(2)
      os.system("echo 2")
      #os.system("ping www.bing.com")

def main():
  jobs = []
  p1 = Process(target=worker1,args=())
  p2 = Process(target=worker2,args=())
  jobs.append(p1)
  jobs.append(p2)
  p1.start()
  p2.start()
  ##to avoid defunct process,you should call join()
  for j in jobs:
    j.join()

if __name__=='__main__':
  main()

Upvotes: 0

Related Questions