小编Ale*_*ana的帖子

并发.futures.ThreadPoolExecutor不打印错误

我正在尝试使用并发.futures.ThreadPoolExecutor 模块并行运行类方法,我的代码的简化版本几乎如下:

class TestClass:

    def __init__(self, secondsToSleepFor):

        self.secondsToSleepFor = secondsToSleepFor


    def testMethodToExecInParallel(self):

        print("ThreadName: " + threading.currentThread().getName())
    

        print(threading.currentThread().getName() + " is sleeping for " + str(self.secondsToSleepFor) + " seconds")
    

        time.sleep(self.secondsToSleepFor)


        print(threading.currentThread().getName() + " has finished!!")


with concurrent.futures.ThreadPoolExecutor(max_workers = 2) as executor:

    futuresList = []

    print("before try")

    try:

         testClass = TestClass(3)        

         future = executor.submit(testClass.testMethodToExecInParallel)

         futuresList.append(future)

    except Exception as exc:

         print('Exception generated: %s' % exc)
Run Code Online (Sandbox Code Playgroud)

如果我执行这段代码,它的行为似乎就像它预期的那样。但是,如果我犯了一个错误,例如在“testMethodToExecInParallel”中指定了错误数量的参数,例如:

 def testMethodToExecInParallel(self, secondsToSleepFor):
Run Code Online (Sandbox Code Playgroud)

然后仍然将函数提交为:

future = executor.submit(testClass.testMethodToExecInParallel)
Run Code Online (Sandbox Code Playgroud)

或者尝试在“testMethodToExecInParallel”方法中的打印语句内将字符串对象与整数对象连接(不使用 str(.) ):

def testMethodToExecInParallel(self):

        print("ThreadName: " + threading.currentThread().getName())
        print("self.secondsToSleepFor: …
Run Code Online (Sandbox Code Playgroud)

python multithreading python-3.x concurrent.futures

6
推荐指数
1
解决办法
8273
查看次数

为什么直接将字符串传递给printf正常工作?

我知道在C中这两个工作:

char* string = "foo";
printf("string value: %s", string);
Run Code Online (Sandbox Code Playgroud)

更简单地说:

printf("string value: %s", "foo");
Run Code Online (Sandbox Code Playgroud)

但我问自己为什么.

我知道%s标识符需要参数为char*,string实际上是(并且它与字符数组相同,因为这两种数据类型在C中非常相似)

但是当我直接传递一个字符串到printf时应该不一样吗?我的意思"foo"是不再是指针......对吗?

c string printf pointers

4
推荐指数
1
解决办法
125
查看次数