1

I'm having a problem with my code. I am using the Simpy for Python and I'm trying to make a P2P simulator utilizing Simpy.

Bellow is the code of my generator of peers, I don't know why but I never enter in the function generate(). The console don't shows the print('I am here').

Anyone knows what I'm doing wrong on my code? Sorry if I'm doing something very wrong.

import simpy
import random

# PARAMETERS
RANDOM_SEED = 93817
N_NODES = 10  # 2000
RUN_TIME = 10  # 86400  # 24 hours
TIME_TO_GENERATE = 3  # at each 3 seconds

# FUNCTIONS


def peer(env, N_PEER):
    print('Peer %d join at %d' % (N_PEER, env.now))


def chanceToGenerate():
    value = random.random()*100
    if value < 50:
        return False
    else:
        return True


def generate(env, N_PEER):
    print('I am here')
    chance = chanceToGenerate()
    if chance:
        yield env.process(peer(env, N_PEER))
        return True
    else:
        return False


def peerGenerator(env):
    N_PEER = 0
    while True:
        if N_PEER < N_NODES:
            generated = generate(env, N_PEER)
            if generated:
                N_PEER += 1
        print('time: %d' % env.now)
        yield env.timeout(TIME_TO_GENERATE)


# RUNNING
random.seed(RANDOM_SEED)
env = simpy.Environment()

env.process(peerGenerator(env))

env.run(until=RUN_TIME)
xGlover
  • 59
  • 7

1 Answers1

0

I solved the problem, what was my solution ? Answer: I removed the function generate() and moved the yield env.process(peer(env, N_PEER)) to the functiongenerator().

Why I did that? I have been reading the documentation of Simpy and I found out that I can't make a non-process yields another process. So only the peerGenerator() function can yields another process.

Code:

import simpy
import random

# PARAMETERS
RANDOM_SEED = 93817
N_NODES = 10  # 2000
RUN_TIME = 10  # 86400  # 24 hours
TIME_TO_GENERATE = 3  # at each 3 seconds

# FUNCTIONS


class peerGenerator:
    def __init__(self, env):
        self.env = env
        self.generator_proc = env.process(self.generator(env))

    def peer(self, env, N_PEER):
        print('Peer %d join at %d' % (N_PEER, env.now))
        yield env.timeout(0)

    def chanceToGenerate(self):
        value = random.randint(0,100)
        if value < 50:
            print('Tried to create a peer')
            return False
        else:
            return True


    def generator(self, env):
        N_PEER = 0
        while True:
            if N_PEER < N_NODES:
                chance = self.chanceToGenerate()
                if chance:
                    yield env.process(self.peer(env, N_PEER))
                    N_PEER += 1
            print('time: %d' % env.now)
            yield env.timeout(TIME_TO_GENERATE)


# RUNNING
env = simpy.Environment()

bootstrap = peerGenerator(env)

env.run(until=RUN_TIME)
xGlover
  • 59
  • 7