相关疑难解决方法(0)

如何调试多线程python脚本

我有一个运行几个线程的python脚本。通常,当我想调试python脚本时,请使用“ -m pdb”运行它,然后使用“ b”设置一个断点。但是,由于某种原因,即使它通过了那条线,它也不会在断点处停止,甚至我看到实际上已经添加了断点。知道我在做什么错吗?我从这里使用了一个简单的python线程模块示例

import threading

class SummingThread(threading.Thread):
     def __init__(self,low,high):
         threading.Thread.__init__(self)
         self.low=low
         self.high=high
         self.total=0

     def run(self):
         print 'self.low = ' + str(self.low) + ', self.high = ' + str(self.high)
         for i in range(self.low,self.high):
             self.total+=i

thread1 = SummingThread(0,500000)
thread2 = SummingThread(500000,1000000)
thread1.start() # This actually causes the thread to run
thread2.start()
thread1.join()  # This waits until the thread has completed
thread2.join()
# At this point, both threads have completed
result = thread1.total + thread2.total
print result
Run Code Online (Sandbox Code Playgroud)

然后run,使用 …

python debugging

5
推荐指数
1
解决办法
2809
查看次数

Python:并行处理列表中的N个文件

有我正在用某种功能一一处理的文件列表:

list_of_files = [file_1, file_2, file_3, .... file_n]

# each file we are processing in function function_x(file)

for file in list_of_files:
   function_x(file)
Run Code Online (Sandbox Code Playgroud)

但处理一个文件需要太长时间,所以我想并行处理 4 个文件,当其中任何一个文件完成后,继续下一个表单 list_of_files

python list

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

多线程中的熊猫数据框

有人可以告诉我一种在多个线程将要使用必须将数据附加到数据帧中的函数的情况下以python方式将数据添加到pandas数据帧中的方法吗?

我的代码从URL抓取数据,然后我使用df.loc [index] ...将被抓取的行添加到数据框中。

自从我启动了一个多线程之后,该线程基本上将每个URL分配给每个线程。简而言之,一次刮掉了许多页面...

如何将这些行附加到数据框中?

python multithreading dataframe pandas

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

Python在单独的线程中调用相同函数的最佳方法是什么?

在单独的线程中调用相同函数的最佳方法是什么,并且每个实例都有一个单独的列表,其中包含返回值,而不重复函数

例:

import threading


def function(a):

    returned_values = []

    ct = threading.currentThread() 
    while getattr(ct, "do_run", True):
        ret = do_something(a)
        returned_values.append(ret)


t1 = threading.Thread(target=function, args=("AAA",))
t2 = threading.Thread(target=function, args=("BBB",))
t3 = threading.Thread(target=function, args=("CCC",))

t1.start()
t2.start()
t3.start()

import time;time.sleep(10)
t1.do_run = t2.do_run = t3.do_run = False
Run Code Online (Sandbox Code Playgroud)

编辑:忘记提到我使用Python 2.7

python python-multithreading python-2.7

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

线程未在 python 脚本中并行运行

我是 python 和线程的新手。我试图一次运行多个线程。这是我的基本代码:

  import threading
  import time
  threads = []

  print "hello"

  class myThread(threading.Thread):
          def __init__(self,i):
                  threading.Thread.__init__(self)
                  print "i = ",i
                  for j in range(0,i):
                          print "j = ",j
                          time.sleep(5)

  for i in range(1,4):
          thread = myThread(i)
          thread.start()
Run Code Online (Sandbox Code Playgroud)

当 1 个线程正在等待时,time.sleep(5)我希望启动另一个线程。简而言之,所有线程都应该并行运行。

python multithreading python-2.7

2
推荐指数
1
解决办法
7994
查看次数