问题记录

1、在for循环下创建线程,会一次性全部创建,消耗内存

import threading

 

def test():

  print('test')

for i in range(1000);

  t = threading.Thread(target=test)

# 此时已经新建了1000个线程对象

 

2、多线程不能同时操作一个类实例

一个类被实例化为一个对象后,该对象的方法不能被多进程,多线程同时调用。

但是,当该方法被赋给一个变量后,就可以被同时调用了。

示例如下:

import threading

 

class Test(object):

  def test():

    print('test')

 

t = Test()

for i in range(4):

  t = threading.Thread(target=t.test)

#  AttributeError: 'Thread' object has no attribute 'test'

x = t.test

for i in range(4):

  t = threading.Thread(target=x)

# ok

 

相关文章:

  • 2021-08-30
  • 2021-09-17
  • 2022-12-23
  • 2021-12-07
  • 2022-02-18
  • 2021-05-22
  • 2022-01-13
  • 2021-06-23
猜你喜欢
  • 2021-10-29
  • 2021-08-01
  • 2022-12-23
  • 2021-11-08
  • 2021-06-19
  • 2021-07-22
  • 2022-03-06
相关资源
相似解决方案