1

I wish to use the arcade library with the twisted library. but they both have a blocking main loop run() function. what should I do?

I tried using threads so the main loops would run simultaneously. but arcade crushes when I try to run it.

ajr120
  • 11
  • 2

1 Answers1

0

You can run arcade in main function and twisted in thread. Example:

import arcade
import threading
from twisted.web import server, resource
from twisted.internet import reactor, endpoints


class Twisted(resource.Resource):
    isLeaf = True
    i = 0

    def render_GET(self, request):
        self.i += 1
        return f'Request #{self.i}'.encode('ascii')


class Arcade(arcade.Window):
    def __init__(self):
        super().__init__(400, 300)
        self.twisted = Twisted()
        endpoints.serverFromString(reactor, 'tcp:8080').listen(server.Site(self.twisted))
        threading.Thread(target=reactor.run, args=(False,)).start()

    def on_draw(self):
        self.clear()
        arcade.draw_text(text=f'Request #{self.twisted.i}', start_x=200, start_y=150, font_size=20, anchor_x='center')


Arcade()
arcade.run()

Open http://localhost:8080/ in browser and simply refresh the page. Arcade window will be showing number of refreshes:

TwistedArcade

Alderven
  • 7,569
  • 5
  • 26
  • 38