コード例 #1
0
import random
import threading

import count_ext                                         #1
import pithreaded                                        #2


class ThreadedPiExt(threading.Thread):
    """The thread.
    """

    def __init__(self, n):
        self.n = n
        super(ThreadedPiExt, self).__init__()

    def count_inside(self):
        """Count all hits inside the circle.
        """
        self.count = count_ext.count(self.n)              #3

    def run(self):
        """Start the thread.
        """
        self.count_inside()



if __name__ == '__main__':

    pithreaded.main(2, int(1e8), ThreadedPiExt)           #4
コード例 #2
0
import threading
import time

import pithreaded


class ThreadedPiSleep(threading.Thread):
    """Thread that just waits for while.
    """
    def __init__(self, n):
        self.n = n
        super(ThreadedPiSleep, self).__init__()
        self.count = 0

    def count_inside(self):
        """No counting, just wait.
        """
        time.sleep(2)                                     #1
        self.count += 1

    def run(self):
        """Start the thread.
        """
        self.count_inside()


if __name__ == '__main__':

    pithreaded.main(thread_class=ThreadedPiSleep)        #2
コード例 #3
0
import pithreaded


class ThreadedPiProcess(threading.Thread):
    """Thread that just waits for while.
    """
    def __init__(self, n):
        self.n = n
        super(ThreadedPiProcess, self).__init__()
        self.count = 0

    def count_inside(self):
        """No counting with eternal process.
        """
        pipe = subprocess.Popen(['count'],
                                shell=True,
                                stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE)
        pipe.stdin.write('%d\n' % self.n)
        self.count = int(pipe.stdout.read())

    def run(self):
        """Start the thread.
        """
        self.count_inside()


if __name__ == '__main__':

    pithreaded.main(n=1e8, thread_class=ThreadedPiProcess)        #2