强制在特定CPU核心上执行线程

时间:2016-01-07 14:30:52

标签: python multithreading

我在python中有两个线程,我想在CPU核心0上强制执行Thread1,在CPU核心1上强制执行Thread2。这可能吗?如果是这样,怎么样?感谢

import threading
import time

#I would like to execute this thread on CPU core 0
class Thread1(threading.Thread):

    def __init__(self):
        self.running = 1;  
        super(Thread1, self).__init__()

    def run(self):
        while self.running:
           print "Thread1"

#I would like to execute this thread on CPU core 1    
class Thread2(threading.Thread):

    def __init__(self):
        self.running = 1;  
        super(Thread2, self).__init__()

    def run(self):
        while self.running:
           print "Thread2"


if __name__ == "__main__":
  thread1 = Thread1()
  thread1.start()

  thread2 = Thread2()
  thread2.start()

  start_time = time.time()

  while (time.time() - start_time) <= 5:
    print "main"

  thread1.running = 0;
  thread2.running = 0;
  thread1.join()
  thread2.join()

就像现在一样,无法确定进程正在运行的核心。

0 个答案:

没有答案