如何在python中使用多重处理将新值分配给变量

时间:2018-08-06 19:02:25

标签: python multiprocessing

public class KafkaController {

    @Autowired
    private KafkaTemplate<String,BankAccount> kafkaTemplate;

    private static final String TOPIC = "my-topic";

    @GetMapping("/publish/{message}")
    public String postKafka(@PathVariable("message") final String message){
        BankAccount bankAccount= new BankAccount();
        bankAccount.setBalance(120.0);

        kafkaTemplate.send(TOPIC,bankAccount);
        return "Succesfully";
    }


}

我希望return new DefaultKafkaConsumerFactory<>( consumerConfigs(), new StringDeserializer(), new JsonDeserializer<>(BankAccount.class)); 的输出为2,但实际上我得到了1。如何为现有变量分配新值?

1 个答案:

答案 0 :(得分:1)

您需要使用multiprocessing shared values;

import multiprocessing
import ctypes

def foo(var, new_value):
    var.value = new_value

x = multiprocessing.Value(ctypes.c_int, 1)
print(x.value)
p = multiprocessing.Process(target=foo, args=(x,2))
p.start()
p.join()
print(x.value)