A little confusing about the wait_for. As I think the total_cpus_expected, total_cpus_qemu and total_cpus_guest are fixed int. So if it not equal with others at the first time it will not equal later. So the wait_for here is useless. You should use len(vm.get_vcpu_pids(params)) and vm.get_cpu_count()  directly in the function.