English 中文(简体)
Python 如何获得线索值?
原标题:Python how do get the value a thread?

Program that determines whether changing the current directory using os.chdir in one thread changes the current directory for a thread that already existed before the call to os.chdir My question is how do I get the value of a live thread?

import threading
import time
import os
class MyThread(threading.Thread):
def __init__(self, *args, **kw):
    threading.Thread.__init__(self, *args, **kw)
    self.sleeptime = 2
def run(self):        
    for i in range(self.sleeptime):
        for j in range(500000):
            k = j*j
        print(self.name, "finished pass", i)
    print(self.name, "finished after", self.sleeptime, "seconds")

bgthreads = threading.active_count()
threadOne = os.chdir("V:\workspace\Python4_Homework10")
threadTwo = os.chdir("V:\workspace")
threadThree = os.chdir("V:")
tt = [MyThread(threadOne), MyThread(threadTwo), MyThread(threadThree)]
for t in tt:    
   t.start()

print("Threads started")
while threading.active_count() > bgthreads:
    time.sleep(2)
    print("tick")
问题回答

有点粗糙,但这样你的工作可能就完成了

#!/usr/bin/env python3

import os
import threading

def threadReporter(i,name):
    if name ==  Thread-changer : # crude, but gets the job done for this
        os.chdir( /tmp )
    print("{0} reports current pwd is: {1}".format(name,os.getcwd()))

if __name__ ==  __main__ :
    # create pre-chdir and changer Thread
    t_pre = threading.Thread(target=threadReporter, args=(1, Thread-pre ))
    t_changer = threading.Thread(target=threadReporter, args=(2, Thread-changer ))

    # start changer thread
    t_changer.start()
    # wait for it to finish
    t_changer.join()

    # start the thread that was created before the os.chdir() call
    t_pre.start()

    # create a thread after the os.chdir call and start it
    t_post = threading.Thread(target=threadReporter, args=(3, Thread-post ))
    t_post.start()

正如上文Gps已经指出的,当前的工作目录是进程-全球目录,而不是为每个可能生成/运行的线索分隔开来。因此,对于每个运行的线索来说,上面程序目前的工作目录报告是相等的,无论线索何时创建。在 os.chdir () 之后,为所有线索设置新的工作目录。





相关问题
Get webpage contents with Python?

I m using Python 3.1, if that helps. Anyways, I m trying to get the contents of this webpage. I Googled for a little bit and tried different things, but they didn t work. I m guessing that this ...

What is internal representation of string in Python 3.x

In Python 3.x, a string consists of items of Unicode ordinal. (See the quotation from the language reference below.) What is the internal representation of Unicode string? Is it UTF-16? The items ...

What does Python s builtin __build_class__ do?

In Python 3.1, there is a new builtin function I don t know in the builtins module: __build_class__(...) __build_class__(func, name, *bases, metaclass=None, **kwds) -> class Internal ...

what functional tools remain in Python 3k?

I have have read several entries regarding dropping several functional functions from future python, including map and reduce. What is the official policy regarding functional extensions? is lambda ...

Building executables for Python 3 and PyQt

I built a rather simple application in Python 3.1 using PyQt4. Being done, I want the application to be distributed to computers without either of those installed. I almost exclusively care about ...

热门标签