Python Forum
function/nonetype object is not iterable - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: function/nonetype object is not iterable (/thread-26604.html)



function/nonetype object is not iterable - nanok66 - May-06-2020

Hi all,

I am getting error "functools.partial object not iterable". I google searched this issue and have read multiple answers about this issue but still cannot figure out how to rewrite my code to get my subprocess to run.

import time
import sched
import subprocess
from functools import partial

s = sched.scheduler(time.time, time.sleep)

def delay(set_time):
    s.enter(set_time, 1, pass_func)
    s.run()

def pass_func():
    pass

def emptyKeg():
    print("A start: ", time.time())
    delay(5)
    print("A after 5: ", time.time())

def emptyGlass():
    print("B start: ", time.time())
    delay(5)
    print("B after 5: ", time.time())

cycle = [emptyKeg(), emptyGlass()]

def runCycle(cycle):
    for func in cycle:
        try:
            func
        except ValueError: 
            break

sub = subprocess.Popen(partial(runCycle, cycle))


while True:
    pass
I've also rewritten the program to be simpler, without using partial and I get error "nonetype object is not iterable". Again, I've read lot about this topic but still not sure what I am doing wrong.

import time
import sched
import subprocess


s = sched.scheduler(time.time, time.sleep)


def delay(set_time):
    s.enter(set_time, 1, pass_func)
    s.run()


def pass_func():
    pass


def emptyKeg():
    print("A start: ", time.time())
    delay(5)
    print("A after 5: ", time.time())


def emptyGlass():
    print("B start: ", time.time())
    delay(5)
    print("B after 5: ", time.time())


def runCycle():
   emptyKeg()
   emptyGlass()


sub = subprocess.Popen(runCycle())


while True:
    pass



RE: function/nonetype object is not iterable - Larz60+ - May-06-2020

please post complete unmodified error traceback


RE: function/nonetype object is not iterable - nanok66 - May-06-2020

Ok my bad.

First traceback:
Error:
Traceback (most recent call last): File "C:/Users/Darwin/mu_code/sampleSubprocess2.py", line 41, in <module> sub = subprocess.Popen(partial(runCycle, cycle)) File "C:\Users\Darwin\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 854, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "C:\Users\Darwin\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 1247, in _execute_child args = list2cmdline(args) File "C:\Users\Darwin\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 549, in list2cmdline for arg in map(os.fsdecode, seq): TypeError: 'functools.partial' object is not iterable
Process finished with exit code 1


Second traceback:
Error:
Traceback (most recent call last): File "C:/Users/Darwin/mu_code/sampleSubprocess2.py", line 36, in <module> sub = subprocess.Popen(runCycle()) File "C:\Users\Darwin\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 854, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "C:\Users\Darwin\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 1247, in _execute_child args = list2cmdline(args) File "C:\Users\Darwin\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 549, in list2cmdline for arg in map(os.fsdecode, seq): TypeError: 'NoneType' object is not iterable
Process finished with exit code 1


RE: function/nonetype object is not iterable - Larz60+ - May-07-2020

I'm not sure what your intent is on line 35 (different from error traceback (35 vs 36)) (second code) as nothing is returned from runCycle()


RE: function/nonetype object is not iterable - deanhystad - May-07-2020

cycle = [emptyKeg(), emptyGlass()] # this is [None, None]
# Maybe
# cycle = [emptyKeg, emptyGlass]
 
def runCycle(cycle):
    for func in cycle:
        try:
            func  # Does not call function.  Maybe func()
        except ValueError: 
            break



RE: function/nonetype object is not iterable - nanok66 - May-08-2020

Cool, changed my use of parenthesis like deanhystad suggested.

Commenting out my subprocess code, this script works now.

import time
import sched
import subprocess
from functools import partial

s = sched.scheduler(time.time, time.sleep)


def delay(set_time):
    s.enter(set_time, 1, pass_func)
    s.run()


def pass_func():
    pass


def emptyKeg():
    print("A start: ", time.time())
    delay(5)
    print("A after 5: ", time.time())


def emptyGlass():
    print("B start: ", time.time())
    delay(5)
    print("B after 5: ", time.time())


def runCycle(cycle):
    for func in cycle:
        try:
            func()
        except ValueError:  
            break

cycleRun = [emptyKeg, emptyGlass]

runCycle(cycleRun)    # new line to replace the broken code below

#sub = subprocess.Popen(partial(runCycle, cycleRun))  # broken, not sure whats wrong

while True:
    pass
Now I just have to figure out how to make subprocess work.

I suppose I should add that I was previously trying to use a thread to run my function runCycle. But now I'm trying to use subprocess instead of thread because subprocess has a method kill() that will quickly stop the code.

Hmm maybe Popen is not the best fit to run a function. I've got to do more reading on this. Thanks everyone for helping with my poor understanding of python!